Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use an array in Laravel update query

I would like to push an array in Where Clause of Laravel Update Query.

Here is the update query.

DB::table('users')->where('id', 1)->update(array('votes' => 1));

Is it possible to use the query like below ??

$array_of_ids;
DB::table('users')->where($array_of_ids)->update(array('votes' => 1));

Thanks

like image 637
julious ceazer Avatar asked Mar 23 '15 09:03

julious ceazer


2 Answers

Simply use whereIn:

$array_of_ids;
DB::table('users')->whereIn('id', $array_of_ids)->update(array('votes' => 1));

Please read the documentation carefully. In this case, all kinds of where statements are documented here: Query Builder - Selects

like image 130
lukasgeiter Avatar answered Oct 12 '22 23:10

lukasgeiter


Try this query:

DB::table('users')->whereIn('id', $array_of_ids)->update(['votes' => 1]);

Using Model:

User::whereIn('id', $array_of_ids)->update(['votes' => 1]);
like image 29
Yasin Patel Avatar answered Oct 13 '22 00:10

Yasin Patel