首页 > 解决方案 > 管理员守卫中的laravel广播

问题描述

在我的 laravel 网站中,我们有两个警卫admincustomer这个警卫的表格是 ->customer tableadmin table

我想通过 这是我在 .js 文件中的代码将通知从 a 发送到customer管理员守卫adminlaravel broadcasting

server.js 文件:

window.Echo = new Echo({

   broadcaster: 'socket.io',
   host: window.location.hostname + ':6001',
}); 

* i have a admin by id=2 *

Echo.private('App.Models.Admin.2')
    .notification((notification) => {
        console.log(notification.type);
    });

并进入客户警卫

\Notification::send(Admin::find(2) , new SimpleAlert('hey bro'));

简单警报.php:

public function toBroadcast($notifiable)
{
    return new BroadcastMessage([
        'message' => $this->message,
        'type' => 'broadcast'
    ]);
}

所以,我启动 laravel-echo-server 并打开两个谷歌浏览器标签,一个用于admin guard另一个用于customer guard并登录到两个警卫。
控制台没有错误。没有错误customer sending notification,但 `admin 没有收到通知。

问题:
当我进入时 laravel-echo-server 显示这个admin guard

客户端无法通过身份验证,获得 HTTP 状态 403

请帮帮我。

标签: phplaravelsocket.iobroadcast

解决方案


您可以做的是在routes/channels.php. 试试这个

Broadcast::channel('App.Models.Admin.{userId}', function ($user, $userId) {
    return $user->id === $userId;
});

推荐阅读