正确答案
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']);
whereIn
will 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 findMany
you were using was executing it thus returning a Collection of models.
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.
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 whereIn
method.
update
on model instances. You can define it yourself, but in the end somewhere in your code you have to writeMyModel::whereIn('id', [1, 2, 3])->update(...)
. – Sergiu Paraschiv Feb 5 '15 at 17:10