What's the syntactically nicest way to bulk update/delete a set of #Laravel Models while still triggering their model observers?
There has to be a better way than this:
Model::query()
->where('field', 'value')
->each(function($model) {
$model>update(['field'='newvalue']);
});
There has to be a better way than this:
Model::query()
->where('field', 'value')
->each(function($model) {
$model>update(['field'='newvalue']);
});
Comments
```
Model::query()
->where('field', 'value')
->each
->update(['field' => 'newvalue']);
```
You could do a mass update, iterate through each object, update whatever was updated in-memory and manually dispatch the event.
I wish there was something like:
Model::withObservers()
->where('field', 'value')
->update([
'field' => 'newvalue'
])
but the query builder doesn't know about the observers. I don't like global helpers, but maybe this?
updateWithObservers($collection, [
'field' => 'newvalue'
]);