我無法讓我的事件在 laravel 7.x、pusher、laravel-echo 上運行。我已經正確設置了所有內容,但仍然收到 401 錯誤。我還在 .env 文件中將broadcast_driver 設置為推送器。如果有人可以提供幫助,我將不勝感激。評論事件.php<?phpnamespace App\Events;use Illuminate\Broadcasting\Channel;use Illuminate\Broadcasting\InteractsWithSockets;use Illuminate\Broadcasting\PresenceChannel;use Illuminate\Broadcasting\PrivateChannel;use Illuminate\Contracts\Broadcasting\ShouldBroadcast;use Illuminate\Foundation\Events\Dispatchable;use Illuminate\Queue\SerializesModels;class CommentEvent implements ShouldBroadcast{ use Dispatchable, InteractsWithSockets, SerializesModels; public $comment; /** * Create a new event instance. * * @return void */ public function __construct($comment) { $this->comment =$comment; } /** * Get the channels the event should broadcast on. * * @return \Illuminate\Broadcasting\Channel|array */ public function broadcastOn() { return new Channel('comment-channel'); } public function broadcastAs() { return 'newComment'; }}CommentController.php<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;use App\Events\CommentEvent;use App\Comment;class CommentController extends Controller{ public function index(){ return view('comments'); } public function fetchComments(){ $comments =Comment::all(); return response()->json($comments); } public function store(Request $request){ $comment =Comment::create($request->all()); event(new CommentEvent($comment)); return response()->json('ok'); }}
1 回答

慕姐4208626
TA貢獻1852條經驗 獲得超7個贊
確保將$user傳遞到通道路由回調中。將此代碼替換為您的頻道路由。那么你就可以走了:)
Broadcast::channel('comment-channel', function ($user) { return true; });
- 1 回答
- 0 關注
- 156 瀏覽
添加回答
舉報
0/150
提交
取消