2

Is there a way to make a Delete with Inner Join on Laravel 4 through a query builder?

I have this sample query (sample query taken from this source.):

DELETE s.* FROM spawnlist s
INNER JOIN npc n ON s.npc_templateid = n.idTemplate
WHERE (n.type = "monster");

I've tried something like:

DB::table('spawnlist as s')
        ->join('npc as n', 's.npc_templateid', '=', 'n.idTemplate')
        ->where('n.type', 'monster')
        ->delete();

But the it seems that the join cannot be done here?

1 Answer 1

0

i think the best way is to us laravel's event, it is more easier and clean. you can perform same task like so:

DB::table('spawnlist')->delete();

then create a spawnlist event catch the deleting event and delete the other table like so:

Spawnlist::deleting( function ($sl) 
{
    DB::table('npc')->delete();
});
Sign up to request clarification or add additional context in comments.

3 Comments

how about the where clause?
You can still add it before the delete method like so... DB::table('spawnlist')->where('table.field', 'table.field')->delete();
Wouldn't this cause the DB facade to query and commit twice to the database?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.