Laravel 4 如何监听模型事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16757616/
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 07:49:29  来源:igfitidea点击:

Laravel 4 how to listen to a model event?

laravellaravel-4eloquent

提问by HymanpotK

I want to have an event listener binding with a model event updating.
For instance, after a post is updated, there's an alert notifying the updated post title, how to write an event listener to have the notifying (with the post title value passing to the listener?

我想要一个与模型 event 绑定的事件侦听器updating
例如,帖子更新后,有一个警报通知更新的帖子标题,如何编写事件侦听器以进行通知(将帖子标题值传递给侦听器?

回答by Rex Schrader

This post: http://driesvints.com/blog/using-laravel-4-model-events/

这篇文章:http: //driesvints.com/blog/using-laravel-4-model-events/

Shows you how to set up event listeners using the "boot()" static function inside the model:

向您展示如何使用模型内的“boot()”静态函数设置事件侦听器:

class Post extends eloquent {
    public static function boot()
    {
        parent::boot();

        static::creating(function($post)
        {
            $post->created_by = Auth::user()->id;
            $post->updated_by = Auth::user()->id;
        });

        static::updating(function($post)
        {
            $post->updated_by = Auth::user()->id;
        });
    }
}

The list of events that @phill-sparks shared in his answer can be applied to individual modules.

@phill-sparks 在他的回答中分享的事件列表可以应用于各个模块。

回答by Phill Sparks

The documentation briefly mentions Model Events. They've all got a helper function on the model so you don't need to know how they're constructed.

该文档简要提到了Model Events。它们在模型上都有一个辅助函数,因此您无需知道它们是如何构建的。

Eloquent models fire several events, allowing you to hook into various points in the model's lifecycle using the following methods: creating, created, updating, updated, saving, saved, deleting, deleted. If false is returned from the creating, updating, saving or deleting events, the action will be cancelled.

Eloquent 模型会触发多个事件,允许您使用以下方法挂钩模型生命周期中的各个点:创建、创建、更新、更新、保存、保存、删除、删除。如果创建、更新、保存或删除事件返回false,则该操作将被取消。



Project::creating(function($project) { }); // *
Project::created(function($project) { });
Project::updating(function($project) { }); // *
Project::updated(function($project) { });
Project::saving(function($project) { });  // *
Project::saved(function($project) { });
Project::deleting(function($project) { }); // *
Project::deleted(function($project) { });

If you return falsefrom the functions marked *then they will cancel the operation.

如果您false从标记的函数中返回,*那么它们将取消操作。



For more detail, you can look through Illuminate/Database/Eloquent/Modeland you will find all the events in there, look for uses of static::registerModelEventand $this->fireModelEvent.

欲了解更多的细节,你可以看看通过照亮/数据库/雄辩/型号,你会发现在那里所有的事件,寻找的用途static::registerModelEvent$this->fireModelEvent

Events on Eloquent models are structured as eloquent.{$event}: {$class}and pass the model instance as a parameter.

Eloquent 模型上的事件被构造为eloquent.{$event}: {$class}并将模型实例作为参数传递。

回答by Sabrina Leggett

I got stuck on this because I assumed subscribing to default model events like Event:listen('user.created',function($user) would have worked (as I said in a comment). So far I've seen these options work in the example of the default model user created event:

我陷入困境是因为我假设订阅默认模型事件,如 Event:listen('user.created',function($user) 会起作用(正如我在评论中所说)。到目前为止,我已经看到这些选项起作用在默认模型用户创建事件的示例中:

//This will work in general, but not in the start.php file
User::created(function($user).... 
//this will work in the start.php file
Event::listen('eloquent.created: User', function($user).... 

回答by Orange_shadow

Event::listen('eloquent.created: ModelName', function(ModelName $model)   {
    //...
})