当通过 laravel 回显服务器广播 public 频道时,可以将变量从前端传递到事件吗?

It possible to pass variable from frontend to event when broadcasting public channel throuhg laravel echo server?

我工作过的代码:

前端代码 resources/js/components/OnlineWall.vue:


        mounted() {

            window.Echo.channel('laravel_database_new-payload')
                .listen('.new-payload-event', (e) => {
                    console.info('listen');
                    console.log(e.payload);
                })

        }

事件代码 app/Events/NewPayload.php:

<?php

namespace 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;
use App\Payload;

class NewPayload implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $payload;

    /**
     * NewPayload constructor.
     * @param Payload $payload
     * @return void
     */
    public function __construct(Payload $payload)
    {
        $this->payload = $payload;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new Channel('new-payload');
    }

    /**
     * Custom broadcast message name
     *
     * @return string
     */
    public function broadcastAs()
    {
        return 'new-payload-event';
    }

}

如果我触发事件:NewPayload::dispatch($testNewPayload); 一切正常。

但是我需要将整数变量从 vue 组件传递给 laravel 事件 (city_id)

如果 city_id 与来自事件(在 $testNewPayload 中)的有效负载模型的 city_id 相同,则触发事件。否则不要触发事件。

有可能吗?

频道为 public、Laravel 7.0

谢谢!

已解决。

mounted() {

            window.Echo.channel(`laravel_database_new-payload.${this.city_id}`)
                .listen('.new-payload-event', (e) => {
                    console.info('listen');
                    console.log(e.payload);
                })

        }
    public function broadcastOn()
    {
        return new Channel('new-payload.'.$this->payload->city_id);
    }