【问题标题】:Eloquent update and event observers雄辩的更新和事件观察者
【发布时间】:2018-12-04 19:46:41
【问题描述】:

我正在制作一个带有 lumen 的 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()
    ]);
}

为什么第一个代码示例不更新表中的条目,为什么观察者不能被解雇?

【问题讨论】:

  • 你注册你的观察员了吗?

标签: laravel lumen


【解决方案1】:

update,它只触发:savingsaved,当它没有修改任何东西时;

这不会触发update 事件,因为它是mass update

$fota_device = Fota_device::where('id', $deviceId)->update(['fieldName' => $value]);

如果$value 与数据库中的值不同,这将触发更新事件:

User::find($id)->update(['fieldName' => $value]);

在您的情况下,$data = []; 是空数组,它没有修改(更新)任何内容;

【讨论】:

    猜你喜欢
    • 2017-12-22
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 2013-05-31
    • 2021-06-30
    • 2017-05-30
    • 2021-09-26
    • 2020-01-30
    相关资源
    最近更新 更多