4

How to delete rows from multiple tables in one query (with left join). The query:

DELETE `deadline`, `job` FROM `deadline` LEFT JOIN `job` ....

So, I try it like this:

DB::table('deadline', 'job')
    ->leftJoin('job', 'deadline.id', '=', 'job.deadline_id')
    ->where('deadline.id', $id)
    ->delete();

Seems that Laravel doesn't support delete from multiple tables with left join.

Is there a supported way or workaround?

1
  • 2
    Not sure if this is a new feature in Eloquent but it works now. I was able to delete with a left joined query. Commented Jun 22, 2017 at 16:00

4 Answers 4

6

It seems that my way is not possible. So, I did it like this.

$q = 'DELETE deadline, job FROM deadline LEFT JOIN job ...where deadline.id = ?';        
$status = \DB::delete($q, array($id));

Documentation: http://laravel.com/docs/database#running-queries

Sign up to request clarification or add additional context in comments.

Comments

1

DB::table(DB::raw('deadline, job')) might work. If it doesn't, you'll have to write the SQL manually and call it via DB::statement().

Comments

1

To make laravel allow a join in a delete is simple - you just need to change the compileDelete function in Illuminate\Database\Query\Grammars\Grammar to this:

public function compileDelete(Builder $query)
{
    $table = $this->wrapTable($query->from);

    $components = implode(' ', array(
        is_array($query->joins) ? $this->compileJoins($query, $query->joins) : '',
        is_array($query->wheres) ? $this->compileWheres($query, $query->wheres) : '',
        is_array($query->limit) ? $this->compilelimit($query, $query->limit) : '',
        is_array($query->offset) ? $this->compileOffset($query, $query->offset) : ''
    ));

    return trim("delete $table from $table ".$components);
}

Then ->delete() will work the way you expect it to. I've already added this as a pull request to the laravel framework repo, so hopefully this might be merged into the next version - just have to see.

2 Comments

Are there any plans of such change to be implemented in Laravel? Cuz its really a shame, and on top no mention of such in docs nor does it throw an exception.
They implemented this feature, view it here. However this compiles in case of the OP to the following SQL: delete from "deadline" where "rowid" in (select "deadline"."rowid" from "deadline" left join "job" on "deadline"."id" = "job"."deadline_id" where "deadline"."id" = ?) assuming that only the first argument for table() is applied.
0
$query = 'DELETE courses,course_contents FROM courses 
          INNER JOIN course_contents ON course_contents.course_id = courses.id  
          WHERE courses.id = ?';

\DB::delete($query, array($id));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.