Laravel,获取BroadcastOn()

时间:2017-02-19 23:54:29

标签: php laravel

我是否需要通过BroadcastOn()函数获取用户对频道名称的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(),所以我可以将其定义为频道名称?

1 个答案:

答案 0 :(得分:1)

  

您可以在下面进行操作,因为您已经在构造函数中添加了$user

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];
    }