laravel Eloquent 模型更新事件未触发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41295032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:59:04  来源:igfitidea点击:

laravel Eloquent model update event is not fired

phplaraveleloquent

提问by northblue

Merry Christmas guys!

小伙伴们圣诞快乐!

I am new to Laravel. Just had a beginner's question, when I am trying to use service provider and model event to log the update information.

我是 Laravel 的新手。刚刚有一个初学者的问题,当我尝试使用服务提供者和模型事件来记录更新信息时。

Was following the online doc: https://laravel.com/docs/5.3/eloquent#events

正在关注在线文档:https: //laravel.com/docs/5.3/eloquent#events

After put all code together, I find that the model event only fire when create the use but never log anything when I edit the user.

将所有代码放在一起后,我发现模型事件仅在创建使用时触发,但在编辑用户时从不记录任何内容。

Did I miss anything? Feel like the $user didn't get assigned properly. Where is it from? from other service provider?

我错过了什么吗?感觉 $user 没有正确分配。这个从哪里来?来自其他服务提供商?

Any explanation or hint will be appreciated!

任何解释或提示将不胜感激!

<?php

namespace App\Providers;

use App\User;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        User::creating(function ($user) {
            Log::info('event creating');
        });

        User::created(function ($user) {
            Log::info('event created');
        });

        User::updating(function ($user) {
            Log::info('event updating');
        });

        User::updated(function ($user) {
            Log::info('event updated');
        });

        User::saving(function ($user) {
            Log::info('event saving');
        });

        User::saved(function ($user) {
            Log::info('event saved');
        });

        User::deleting(function ($user) {
            Log::info('event deleting');
        });

        User::deleted(function ($user) {
            Log::info('event deleted');
        });
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

回答by Eric Tucker

You need to retrieve the user from the database and then save that user in order to fire the event. For example:

您需要从数据库中检索用户,然后保存该用户以触发事件。例如:

This will NOT fire the update event:

这不会触发更新事件:

User::where('id', $id)->update(['username' => $newUsername]);

This will fire the update event:

这将触发更新事件:

User::find($id)->update(['username' => $newUsername]);

回答by Lionel Chan

Possible reasons:

可能的原因:

  1. The row is not updated at all - no changes. Hence not firing, and

  2. You used update. Check the docs here: https://laravel.com/docs/5.3/eloquent#updates

  1. 该行根本没有更新 - 没有变化。因此不开火,并且

  2. 你用过update。在此处查看文档:https: //laravel.com/docs/5.3/eloquent#updates

When issuing a mass update via Eloquent, the saved and updated model events will not be fired for the updated models. This is because the models are never actually retrieved when issuing a mass update.

当通过 Eloquent 发布批量更新时,不会为更新的模型触发保存和更新的模型事件。这是因为在发布批量更新时实际上从未检索到模型。