1

I'm trying to figure if i could emit an event from a Laravel regular controller and define a listener on a Livewire component. There's my journey so far:

I've generated the event:

php artisan make:event NewOrder

then, on the Controller method placeNewOrder i'm triggering the event like this:

event(new NewOrder())

On the Livewire component i define a varible $showNewOrderNotification and the listener:

public $showNewOrderNotification = false

protected $listeners = ['NewOrder' => 'notifyNewOrder'];

and setting the method notifyNewOrder

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

the livewire view is only showing an alert when the $showNewOrderNotification variable is true:

@if($showNewOrderNotification)
  <div class="alert alert-danger text-uppercase">
    New Orders !!!
  </div>
@endif

This should be pretty straight forward but for some reason the listener is not getting the event.

I appreciate all the help.

JS_LnMstr
  • 348
  • 1
  • 6
  • 18
  • No, isn't possible. Instead, look at some pusher solution to emit the event from laravel controller, get it from pusher and using laravel-echo listen it in livewire. – Prospero Nov 20 '21 at 16:50
  • 1
    Why not convert the controller into a Livewire component? Then you can emit it from there, given that the two components (the one emitting and the one listening) are loaded on that page. Otherwise you need a websocket solution like suggested by Prospero – Qirel Nov 20 '21 at 18:40

1 Answers1

0

user1 = order processor
user2 = customer

Scenario 1:
If user2 places a new order and you're trying to update user1's interface, you'll need to look into broadcasting to get your livewire component to pickup the event for user1.

https://laravel.com/docs/8.x/broadcasting
https://laravel-livewire.com/docs/2.x/laravel-echo

Scenario 2:
If user2 places a new order and you're trying to update user2's interface, it would probably be best to just convert the controller to an entire livewire component and avoid the controller all together. It can be done just storing session variables and checking for those variables using wire:init in your livewire blade view, but much easier to just do it all in a livewire component to begin with.

Hood
  • 1