Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

andiliang's avatar

How to pass laravel echo event to livewire method

Hi ,

inside the livewire doc https://laravel-livewire.com/docs/2.x/laravel-echo we can use below codes to listen from pusher

public function getListeners()
    {
        return [
            "echo-private:orders.{$this->orderId},OrderShipped" => 'notifyNewOrder',
            // Or:
            "echo-presence:orders.{$this->orderId},OrderShipped" => 'notifyNewOrder',
        ];
    }

    public function notifyNewOrder()
    {
        $this->showNewOrderNotification = true;
    }

aboove its the same as below

window.Echo.private('orders')
        .listen('OrderShipped', (e) => {
            console.log(e.order.name);
        });

what if I want to pass the OrderShipped event to livewire fucntion notifyNewOrder ? How can I achieve this

thanks

0 likes
8 replies
andiliang's avatar

yep this one might work , because calling livewire event from the js part , anyway we can do it inside the livewire component ? if no I have to implement like the way you show. thanks

rodrigo.pedra's avatar

Sorry I might have misunderstood then. I thought you wanted to do in JavaScript.

Have you already tried accepting the event payload?

    public function notifyNewOrder($event)
    {
        dd($event);
    }

https://laravel-livewire.com/docs/2.x/events#passing-parameters

At the end they are just regular event listeners with a special syntax that Livewire autowires to Echo events.

If you look at Livewires' github repo you will see every time a listener is called, it is called with any acompanying parameters.

2 likes
RhysLees's avatar

If you are using the broadcast as function then you need to add a period in front of the event name and it will work

EVENT:


    public function broadcastAs()
    {
        return 'OrderShipped';
    }

COMPONENT:

public function getListeners()
    {
        return [
            "echo-private:orders.{$this->orderId},.OrderShipped" => 'notifyNewOrder',
        ];
    }

    public function notifyNewOrder()
    {
        $this->showNewOrderNotification = true;
    }

If you are NOT using the broadcasts and using Laravel's default naming for events then WHAT YOU posted originally will work

COMPONENT:


public function getListeners()
    {
        return [
            "echo-private:orders.{$this->orderId},OrderShipped" => 'notifyNewOrder',
        ];
    }

    public function notifyNewOrder()
    {
        $this->showNewOrderNotification = true;
    }

The reason for this is that livewire automatically adds App\Events\ infront of the event name so although you type

"echo-private:orders.{$this->orderId},OrderShipped" => 'notifyNewOrder',

it is interpreted as

"echo-private:orders.{$this->orderId},App\Events\OrderShipped" => 'notifyNewOrder',

unless you put the . which circumvents it from prepending `App\Events

3 likes
Oddman's avatar

You sonofabitch. hahaha. That's exactly the detail I needed - custom events requiring the dot.

How is that not documented?! Wasted an entire day! lol

Please or to participate in this conversation.