Thus, according to the laravel doc event, when defining listeners, it can receive an instance of the event in the descriptor method and any logic must be executed:
public function handle(FoodWasPurchased $event)
So, if my FoodWasPurchased event is defined below (it is assumed that an EventServiceProvider is set):
public function __construct(Food $food) { $this->food = $food; }
I could access $ food in the event from the listener by doing:
$event->food->doSomething();
But now my question is, what if the listener listens for several events?
Event FoodWasPurchased -> Listener Bill Event DrinksWasPurchased -> Listener Bill
Now I did not specify an instance of the event in the listener method method:
public function handle($event)
where can I use the if condition later to check what is received in the $ event:
if (isset($event->food)) { // Do something... } elseif (isset($event->drinks)) { // Do something else... }
I am sure there is a better way.
Or is it best to make sure that one listener is only listening to one event?
events laravel laravel-5
Park lai
source share