6

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

1
  • use whereIn to update Commented Mar 23, 2015 at 9:44

3 Answers 3

16

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

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @lukasgeiter. But this query updates another field also which contain datetime data.
Are you sure? I'm pretty certain this query should work. What exactly gets updated?
query is working but it also updates another field containing datatime data.
Sounds to me like you might have set up the database field so it sets the current time when inserting/updating automatically...
4

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]);

1 Comment

This one works. Great
0

using the query builder: The query builder can also update existing records using the update method. The update method, like the insert method, accepts an array of column and value pairs containing the columns to be updated. You may constrain the update query using where clauses:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.