我想要一个与模型事件 updating
绑定(bind)的事件监听器.
例如,帖子更新后,有一个警报通知更新的帖子标题,如何编写事件监听器以进行通知(将帖子标题值传递给监听器?
最佳答案
这个帖子:
http://driesvints.com/blog/using-laravel-4-model-events/
向您展示如何使用模型内的“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;
});
}
}
@phill-sparks 在他的回答中分享的事件列表可以应用于各个模块。
关于Laravel 4 如何监听模型事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16757616/