【发布时间】:2021-01-19 11:07:46
【问题描述】:
我有一个会在多个渠道上发送信息的事件:
-
members的频道 -
managers的频道
这是我写的:
<?php
class ModelUpdated implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $model;
public function __construct(Model $model)
{
$this->model = $model;
}
public function broadcastWith($who)
{
if ($who == "model.{$this->model->id}")
return [$this->model->id];
else if ($who == "model.{$this->model->id}.managers")
return [$this->model];
else
return [];
}
public function broadcastOn()
{
return [
new PrivateChannel("model.{$this->model->id}"),
new PrivateChannel("model.{$this->model->id}.managers")
];
}
}
不幸的是,broadcastWith 不适用于我的 $who 魔法。有其他方法吗?
我想避免出现不同的事件,因为我的事件是在模型中触发的:
class MyModel extends Model
{
use Notifiable;
protected $dispatchesEvents = [
'saved' => ModelUpdated::class,
'updated' => ModelUpdated::class,
];
}
【问题讨论】:
-
代码中的模型是什么?
-
一个雄辩的模型