作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要通过 BroadcastOn() 函数获取 channel 名称的用户 ID 吗?
我想做的是:
新事件:
event(new \App\Events\chat($message, $userid, $avatar, $username, $receiver, $msgid));
事件本身
public $message;
public $user;
public $avatar;
public $username;
public $receiver;
构建:
public function __construct($message, $user, $avatar,$username,$receiver,$msgid) {
$this->message = $message;
$this->user = $user;
$this->avatar = $avatar;
$this->username = $username;
$this->receiver = $receiver;
$this->msgid = $msgid;
}
广播:
public function broadcastOn() {
$user = \Auth::user()->id();
return new Channel($user);
}
如果我尝试 dd\Auth::user();它给了我 null,我想这是通过 auth 在事件中无法识别用户。有没有办法以某种方式将用户传递给 broadcastOn() 以便我可以将其定义为 channel 名称?
最佳答案
You can do like below as you have
$user
already in constructor
public function broadcastOn() {
{
return ['channel_'.$this->user->id];
}
或者传递用户id
public function __construct($message, $user, $avatar,$username,$receiver,$msgid,$userid) {
$this->message = $message;
$this->user = $user;
$this->avatar = $avatar;
$this->username = $username;
$this->receiver = $receiver;
$this->msgid = $msgid;
$this->userid= $userid;
}
然后
public function broadcastOn() {
{
return ['channel_'.$this->userid];
}
关于php - Laravel,获取 BroadcastOn() 的用户 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42334394/
我需要通过 BroadcastOn() 函数获取 channel 名称的用户 ID 吗? 我想做的是: 新事件: event(new \App\Events\chat($message, $useri
我是一名优秀的程序员,十分优秀!