Eloquent 更新和事件观察者
Eloquent update and event observers
我正在制作一个带流明的 API。我正在尝试更新条目并触发更新的观察者。
到目前为止我尝试了什么
$data = [];
$fota_device = Fota_device::find($deviceId);
$fota_device->update($data);
此代码不更新数据库或触发更新事件。
$data = [];
$fota_device = Fota_device::where('id', $deviceId);
$fota_device->update($data);
此代码更新数据库但也不会触发事件。
我读到 eloquent 不会触发批量分配的更新事件,但其中一种方式至少应该触发事件但不会。
我的观察者
public function updated(Device $device)
{
dd($fota_device);
$user = Auth::user();
$action = Users_action::create([
'userId' => $user->id,
'created_at' => \Carbon\Carbon::now()->toDateTimeString()
]);
}
为什么第一个代码示例不更新 table 中的条目,为什么不能解雇观察者?
在update
,它只触发:saving
,saved
当它没有修改任何东西时;
这不会触发 update
事件,因为它是 mass update:
$fota_device = Fota_device::where('id', $deviceId)->update(['fieldName' => $value]);
如果 $value
与数据库中的值不同,这将触发更新事件:
User::find($id)->update(['fieldName' => $value]);
在你的例子中,$data = [];
是一个空数组,它没有修改(更新)任何东西;
我正在制作一个带流明的 API。我正在尝试更新条目并触发更新的观察者。 到目前为止我尝试了什么
$data = [];
$fota_device = Fota_device::find($deviceId);
$fota_device->update($data);
此代码不更新数据库或触发更新事件。
$data = [];
$fota_device = Fota_device::where('id', $deviceId);
$fota_device->update($data);
此代码更新数据库但也不会触发事件。 我读到 eloquent 不会触发批量分配的更新事件,但其中一种方式至少应该触发事件但不会。
我的观察者
public function updated(Device $device)
{
dd($fota_device);
$user = Auth::user();
$action = Users_action::create([
'userId' => $user->id,
'created_at' => \Carbon\Carbon::now()->toDateTimeString()
]);
}
为什么第一个代码示例不更新 table 中的条目,为什么不能解雇观察者?
在update
,它只触发:saving
,saved
当它没有修改任何东西时;
这不会触发 update
事件,因为它是 mass update:
$fota_device = Fota_device::where('id', $deviceId)->update(['fieldName' => $value]);
如果 $value
与数据库中的值不同,这将触发更新事件:
User::find($id)->update(['fieldName' => $value]);
在你的例子中,$data = [];
是一个空数组,它没有修改(更新)任何东西;