Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.2. DB table + Join with Update

I need update key in table catalog and I write query (mysql query is right):

update attributes a inner join catalog c on a.parent_id = c.id set a.key = c.left_key

and with Laravel DB:

DB::table('attributes as a')
    ->join(catalog as c', 'a.parent_id', '=', 'c.id')
    ->update([ 'a.key' => 'c.left_key' ]);

It's right query, but DB::table ... make all key in table catalog to 0. I write

DB::statement('update attributes a inner join catalog c on a.parent_id = c.id set a.key = c.left_key;');

And it's work! But I don't understand why DB::table with join and update don't work

like image 952
Misha Babich Avatar asked Oct 29 '16 14:10

Misha Babich


1 Answers

You can try as:

DB::table('attributes as a')
   ->join('catalog as c', 'a.parent_id', '=', 'c.id')
   ->update([ 'a.key' => DB::raw("`c`.`left_key`") ]);
like image 111
Amit Gupta Avatar answered Nov 17 '22 00:11

Amit Gupta