Laravel 向推送者广播消息
Laravel broadcast message to pusher
我试图在 laravel 中通过推送器广播消息,但出现以下错误:
Symfony\Component\Debug\Exception\FatalThrowableError · Too few arguments to function
App\Providers\BroadcastServiceProvider::{closure}(), 1 passed in
laravel/framework/src/Illuminate/Broadcasting/Broadcasters/Broadcaster.php on line 77 and exactly 2
expected routes/channels.php:33App\Providers\BroadcastServiceProvider::{closure}
});
发生的代码如下
Broadcast::channel('conversations.{id}', function ($user, $id) {
return $user->inConversation($id);
});
我有另一个频道,它工作正常
Broadcast::channel('users.{id}', function ($user, $id){
return (int) $user->id === (int) $id;
});
不确定为什么参数太少
*** 更新 ***
我正在使用 laravel livewire。
事件class如下:
class MessageAdded implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $message;
/**
* Create a new event instance.
*
* @param Message $message
*/
public function __construct(Message $message)
{
$this->message = $message;
}
public function broadcastWith()
{
return [
'message' => [
'id' => $this->message->id
]
];
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('conversations.' . $this->message->conversation->id);
}
}
laravellivewire函数如下:
public function reply()
{
$this->validate([
'body' => 'required'
]);
$message = $this->conversation->messages()->create([
'user_id' => auth()->id(),
'body' => $this->body
]);
$this->conversation->update([
'last_message_at' => now()
]);
foreach ($this->conversation->others as $user) {
$user->conversations()->updateExistingPivot($this->conversation, [
'read_at' => null
]);
}
broadcast(new MessageAdded($message))->toOthers();
$this->emit('message.created', $message->id);
$this->body = '';
}
问候
丹尼
基本上,这里的解决方案是将 BroadcastServiceProvider 中的 Broadcast::routes 设置为以下
Broadcast::routes(['middleware' => 'auth']);
我试图在 laravel 中通过推送器广播消息,但出现以下错误:
Symfony\Component\Debug\Exception\FatalThrowableError · Too few arguments to function
App\Providers\BroadcastServiceProvider::{closure}(), 1 passed in
laravel/framework/src/Illuminate/Broadcasting/Broadcasters/Broadcaster.php on line 77 and exactly 2
expected routes/channels.php:33App\Providers\BroadcastServiceProvider::{closure}
});
发生的代码如下
Broadcast::channel('conversations.{id}', function ($user, $id) {
return $user->inConversation($id);
});
我有另一个频道,它工作正常
Broadcast::channel('users.{id}', function ($user, $id){
return (int) $user->id === (int) $id;
});
不确定为什么参数太少
*** 更新 *** 我正在使用 laravel livewire。
事件class如下:
class MessageAdded implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $message;
/**
* Create a new event instance.
*
* @param Message $message
*/
public function __construct(Message $message)
{
$this->message = $message;
}
public function broadcastWith()
{
return [
'message' => [
'id' => $this->message->id
]
];
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('conversations.' . $this->message->conversation->id);
}
}
laravellivewire函数如下:
public function reply()
{
$this->validate([
'body' => 'required'
]);
$message = $this->conversation->messages()->create([
'user_id' => auth()->id(),
'body' => $this->body
]);
$this->conversation->update([
'last_message_at' => now()
]);
foreach ($this->conversation->others as $user) {
$user->conversations()->updateExistingPivot($this->conversation, [
'read_at' => null
]);
}
broadcast(new MessageAdded($message))->toOthers();
$this->emit('message.created', $message->id);
$this->body = '';
}
问候 丹尼
基本上,这里的解决方案是将 BroadcastServiceProvider 中的 Broadcast::routes 设置为以下
Broadcast::routes(['middleware' => 'auth']);