Laravel широковещательное сообщение толкателю - PullRequest
1 голос
/ 03 августа 2020

Я пытаюсь передать сообщение через толкатель в 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 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);
    }
}

Функция laravel livewire выглядит следующим образом:

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 = '';
    }

С уважением, Дэнни

1 Ответ

0 голосов
/ 11 августа 2020

По сути, решение здесь заключалось в том, чтобы установить Broadcast :: routes в BroadcastServiceProvider на следующее

Broadcast::routes(['middleware' => 'auth']);
...