php 在 Laravel 更新查询中使用数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29207055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:18:44  来源:igfitidea点击:

Use an array in Laravel update query

phpmysqlsqllaravel-5

提问by julious ceazer

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

我想在Laravel 更新查询的Where 子句中推送一个数组。

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

谢谢

回答by lukasgeiter

Simply use whereIn:

只需使用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 wherestatements are documented here: Query Builder - Selects

请仔细阅读文档。在这种情况下,所有类型的where语句都记录在此处:查询生成器 - 选择

回答by Paul Caleb

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:

使用查询构建器:查询构建器还可以使用更新方法更新现有记录。update 方法与 insert 方法一样,接受包含要更新的列的列和值对数组。您可以使用 where 子句限制更新查询:

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