php Eloquent - 更新集合中的所有模型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28349929/
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-25 23:48:39  来源:igfitidea点击:

Eloquent - Updating all models in a collection

phplaraveleloquent

提问by Jarry

I want to set a certain attribute in all the models of a collection.

我想在集合的所有模型中设置某个属性。

in plain SQL:

在普通 SQL 中:

UPDATE table SET att = 'foo' WHERE id in (1,2,3)

the code i have:

我有的代码:

$models = MyModel::findMany([1,2,3]);
$models->update(['att'=>'foo']);

taken from here

取自这里

but doesn't work. I'm getting

但不起作用。我越来越

Call to undefined method Illuminate\Database\Eloquent\Collection::update()

the only way i have found it's building a query with the query builder but i'd rather avoid that.

我发现它使用查询构建器构建查询的唯一方法,但我宁愿避免这种情况。

回答by Matt Burrow

You are returning a collection, not keeping the query open to update. Like your example is doing.

您正在返回一个集合,而不是保持查询打开以进行更新。就像你的例子正在做的那样。

$models = MyModel::whereIn('id',[1,2,3]);
$models->update(['att'=>'foo']);

whereInwill query a column in your case id, the second parameter is an array of the ids you want to return, but will not execute the query. The findManyyou were using was executing it thus returning a Collection of models.

whereIn将在您的情况下查询一列id,第二个参数是您要返回的 id 的数组,但不会执行查询。在findMany您使用正在执行它并返回的模型的集合。

If you need to get the model to use for something else you can do $collection = $models->get();and it will return a collection of the models.

如果您需要将模型用于其他用途,您可以执行此操作$collection = $models->get();,它将返回模型的集合。

If you do not just simply write it on one line like so;

如果你不只是像这样简单地把它写在一行上;

MyModel::whereIn('id',[1,2,3])->update(['att'=>'foo']);

Another option which i do not recommend is using the following;

我不推荐的另一个选项是使用以下选项;

$models = MyModel::findMany([1,2,3]);

$models->each(function ($item){
    $item->update(['att'=>'foo']);
});

This will loop over all the items in the collection and update them individually. But I recommend the whereInmethod.

这将遍历集合中的所有项目并单独更新它们。但是我推荐这个whereIn方法。

回答by Marcel

The best solution in one single query is still:

单个查询中的最佳解决方案仍然是:

MyModel::whereIn('id',[1,2,3])->update(['att'=>'foo']);

If you already have a collection of models and you want to do a direct update you can use modelKeys()method. Consider that after making this update your $modelscollection remains outdated and you may need to refresh it:

如果您已经有一组模型并且想要进行直接更新,则可以使用modelKeys()方法。考虑到在进行此更新后,您的$models集合仍然过时,您可能需要刷新它:

MyModel::whereIn('id', $models->modelKeys())->update(['att'=>'foo']);
$models = MyModel::findMany($models->modelKeys());

The next example I will not recommend because for every item of your $modelscollection a new extra query is performed:

我不会推荐下一个示例,因为对于您的$models集合中的每个项目,都会执行一个新的额外查询:

$models->each(function ($item) {
    $item->update(['att'=>'foo']);
});

or simpler, from Laravel 5.4you can do $models->each->update(['att'=>'foo']);

或者更简单,从Laravel 5.4你可以做$models->each->update(['att'=>'foo']);

However, the last example (and only the last) is good when you want to trigger some model events like saving, saved, updating, updated. Other presented solutions are touching direct the database but models are not waked up.

但是,当您想要触发某些模型事件(如saving, saved, updating, )时,最后一个示例(并且仅是最后一个)是很好的updated。其他提出的解决方案直接接触数据库,但模型没有被唤醒。