I want to update multiple rows with one update query. I can do this without bindings like such:
Product::whereIn('id',[1,2,3])->update([
'stock' => DB::raw('CASE id WHEN 1 THEN 1000 WHEN 2 THEN 1001 WHEN 3 THEN 1003 END')
]);
I would however like to use data bindings for the CASE statement. I have tried:
Product::whereIn('id',[1,2,3])->setBindings([1,1,2,2,3,3])->update([
'stock' => DB::raw('CASE id WHEN ? THEN ? WHEN ? THEN ? WHEN ? THEN ? END')
]);
Product::whereIn('id',[1,2,3])->update([
'stock' => DB::raw('CASE id WHEN ? THEN ? WHEN ? THEN ? WHEN ? THEN ? END')
])->setBindings([1,1,2,2,3,3]);
Which both produce the error:
Illuminate/Database/QueryException with message 'SQLSTATE[HY093]: Invalid parameter number (SQL:
update `products` set `stock` = CASE id WHEN 2020-03-19 08:58:16 THEN 1 WHEN 2 THEN 3 WHEN ? THEN ? END, `products`.`updated_at` = ? where `id` in (?, ?, ?) and `products`.`deleted_at` is null)'
I have also tried:
Product::whereIn('id',[1,2,3])->update([
'stock' => DB::raw('CASE id WHEN ? THEN ? WHEN ? THEN ? WHEN ? THEN ? END')
->setBindings([1,1,2,2,3,3])
]);
Which produces the following error:
PHP Error: Call to undefined method Illuminate/Database/Query/Expression::setBindings() in Psy Shell code on line 1