gpt4 book ai didi

php - Laravel,获取 BroadcastOn() 的用户 ID

转载 作者:行者123 更新时间:2023-12-04 17:53:34 24 4
gpt4 key购买 nike

我需要通过 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com