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?

Community
  • 1
  • 1
Vainglory07
  • 5,073
  • 10
  • 43
  • 77

1 Answers1

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();
});
Canaan Etai
  • 3,455
  • 2
  • 21
  • 17