[英]Laravel 5.3 Broadcast using Laravel Echo via Pusher using Models Creating, Saving Etc events
Is there a way to implement this 有没有办法实现这一目标
as documented in this link Event of Models 如此链接中所述的模型事件
How can i extend those events to fire broadcast pusher data, rather than making separate events for the model 我如何扩展这些事件以触发广播的推送程序数据,而不是为模型创建单独的事件
You could do it a number of ways. 您可以通过多种方式来实现。 If you want to catch ALL model events you can register a wildcard listener. 如果要捕获所有模型事件,则可以注册通配符侦听器。 In your App\\Providers\\EventServiceProvider
in the boot method register the wildcard listener: 在启动方法的App\\Providers\\EventServiceProvider
中,注册通配符侦听器:
public function boot()
{
parent::boot();
Event::listen('eloquent.*', function (array $data) {
broadcast(new YourModelEventListener($data));
});
}
Or if you want to keep the logic separate for each model you could create a class observer catch all the events and send to your broadcast handler. 或者,如果您想将每种模型的逻辑分开,则可以创建一个类观察器来捕获所有事件并发送到广播处理程序。
Register the observer 注册观察员
class MyModel extends Model
{
protected static function boot()
{
parent::boot();
static::observe(new MyModelObserver);
}
}
Then in the observer: 然后在观察者中:
class MyModelObserver
{
public function broadcast($method, $model)
{
broadcast(new YourModelEventListener($method, $model));
}
public function creating($model)
{
$this->broadcast('creating', $model);
}
public function updating($model)
{
$this->broadcast('updating', $model);
}
}
public MyBroadCastEvent implements ShouldBroadcast {
public function broadcastOn() {
return ['test'];
}
}
public MyBroadCastEventListener {
public function handle(MyBroadCastEvent $event) {
// should i remove that type hint?
// then do something here
}
}
then call this? 那叫这个
broadcast(new MyBroadCastEventListener($data)); 广播(新的MyBroadCastEventListener($ data)); ? ?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.