Laravel 4怎么听模特儿的事件? [英] Laravel 4 how to listen to a model event?

查看:96
本文介绍了Laravel 4怎么听模特儿的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个事件监听器绑定与模型事件更新

例如,一个帖子更新后,有一个警报通知更新的帖子标题,如何写一个事件监听器来通知(将标题值传递给监听者?

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?

推荐答案

文档简要介绍了模型事件,他们在模型上都有一个帮助功能所以你不需要知道它们的构造方式。

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.


雄辩的模型可以触发几个事件,模型的生命周期使用以下方法:创建,创建,更新,更新,保存,保存,删除,删除。如果从创建,更新,保存或删除事件返回false,则该操作将被取消。

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.







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) { });

如果您返回 false 功能标记为 * ,那么他们将取消操作。

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

有关更多详细信息,您可以查看照明/数据库/雄辩/模型,你会发现所有的事件在那里,寻找使用 static :: registerModelEvent $ this-> fireModelEvent

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

雄辩模型上的活动结构为雄辩。{$ event}:{ $ class} 并将模型实例作为参数传递。

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

这篇关于Laravel 4怎么听模特儿的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆