如何为所有模型而不是单独实施雄辩的“保存"事件 [英] How to implement eloquent 'saving' event for all models , not individually
问题描述
基于 laravel口才事件的文档,所有口才事件都单独触发根据每个模型,是否有任何方法可以使用正在创建"事件或所有其他模型触发的雄辩事件
Based on documentation of laravel eloquent event, all the eloquent event are triggered individually based on each model, is there any way to use 'creating' event or any other eloquent event to be triggered by all the Models
例如,如果创建了任何模型,则会触发事件A
For example , if any Models is created , event A is triggered
推荐答案
收听被口才的creating
事件.这是一个'string'事件,仍然不是一个对象,因此您可以在此处进行一些通配符匹配.
Listen for the Eloquent creating
event that is fired. It is a 'string' event still not an object so you can do some ... wildcard matching here.
这是Eloquent触发的事件的字符串:
This is the string of the events that are fired from Eloquent:
"eloquent.{$event}: ".static::class
因此,您可以侦听"eloquent.creating:*"以捕获所有针对任何Model的Eloquent创建的所有字符串事件.
So you could listen for "eloquent.creating: *" to catch all those string events for Eloquent creating for any Model.
Event::listen('eloquent.creating: *', function ($event, $model) {
....
});
很显然,如果您已将模型定义为使用自定义事件对象,则将无法捕获这些事件.
Obviously if you have defined your model to use custom event objects this would not be able to catch those.
您还可以使模型成为观察者,但是您必须将其注册到要捕获事件的应用程序中的每个模型.
You also could make a model observer, but you would have to register it to every model in your application you wanted to catch events for.
这篇关于如何为所有模型而不是单独实施雄辩的“保存"事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!