Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete rows with Laravel query builder and LEFT JOIN

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?

like image 409
tasmaniski Avatar asked Feb 13 '23 22:02

tasmaniski


2 Answers

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

like image 181
tasmaniski Avatar answered Feb 15 '23 10:02

tasmaniski


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().

like image 39
Andreas Avatar answered Feb 15 '23 12:02

Andreas