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.
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.