P粉1655228862023-08-09 09:30:12
When using a query builder instance or batch update, the event listener will not fire even if you only operate on one row
Equipment::where('id',$id) //right here you get a Builder instance ->update([]); //will not trigger the event listener as it is a mass update
To trigger it, you need to use a model instance
Equipment::where('id',$id)->first() //This returns a model instance ->update([]); //will trigger the event listener as it is a single instance update
This is also equivalent to
$equipment = Equipment::find($id); $equipment->parent_id = ..; $equipment->save();
And you can see that calling update() on the model is different from calling update() on the query builder.
When you consider that to trigger these events, the code needs an instance of the model to work with, like static::updated(function (Model $model) {. If your query is different, for example Equipment: :where('id','>',$id), in order to handle all events, it needs to query all affected rows, generate model instances for them, and then call the event using these models.
This Will reduce performance
But if there is no other way, you can do it explicitly
$equipments = Equipment::where('id','>',$id)->get(); foreach ($equipments as $equipment) { $equipment->update(['parent_id'=>$recordTarget['id']]); }