0
0
Laravelframework~20 mins

Event dispatching in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Laravel Event Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when an event is dispatched with multiple listeners?

Consider a Laravel event dispatched with two listeners attached. What is the expected behavior when the event is fired?

Laravel
Event::dispatch(new UserRegistered($user));

// Two listeners are registered for UserRegistered event
ABoth listeners run in the order they were registered, one after the other.
BOnly the first listener runs; the second is ignored.
CListeners run in parallel and may complete in any order.
DThe event dispatch throws an error if multiple listeners exist.
Attempts:
2 left
💡 Hint

Think about how Laravel handles event listeners synchronously by default.

📝 Syntax
intermediate
2:00remaining
Which code correctly dispatches an event in Laravel?

Choose the correct syntax to dispatch a Laravel event named OrderShipped with an $order object.

AEvent::fire(new OrderShipped($order));
Bdispatch_event(OrderShipped($order));
Cevent(new OrderShipped($order));
DEvent::dispatch(OrderShipped);
Attempts:
2 left
💡 Hint

Remember the helper function Laravel provides for dispatching events.

🔧 Debug
advanced
3:00remaining
Why does this event listener not run when the event is dispatched?

Given this listener class, why might it not be triggered when the event is dispatched?

Laravel
class SendWelcomeEmail implements ShouldQueue
{
    public function handle(UserRegistered $event)
    {
        Mail::to($event->user->email)->send(new WelcomeEmail());
    }
}

// Event dispatched as: event(new UserRegistered($user));
AThe event is dispatched incorrectly; it should use Event::dispatch().
BThe event class name is misspelled in the listener handle method.
CThe listener class does not implement the Listener interface.
DThe listener is queued but the queue worker is not running.
Attempts:
2 left
💡 Hint

Think about what happens when a listener implements ShouldQueue.

state_output
advanced
2:00remaining
What is the value of $count after dispatching the event?

Consider this code snippet:

class CounterListener
{
    public static int $count = 0;

    public function handle(SomeEvent $event)
    {
        self::$count++;
    }
}

Event::listen(SomeEvent::class, [CounterListener::class, 'handle']);

Event::dispatch(new SomeEvent());
Event::dispatch(new SomeEvent());

$count = CounterListener::$count;

What is the value of $count after running this code?

A2
B1
C0
DThrows an error because static property access is invalid.
Attempts:
2 left
💡 Hint

Each event dispatch triggers the listener once.

🧠 Conceptual
expert
3:00remaining
What error occurs if an event class constructor requires a parameter but none is passed during dispatch?

Given an event class PaymentProcessed with a constructor requiring a $paymentId parameter, what happens if you dispatch it without arguments like event(new PaymentProcessed());?

Laravel
class PaymentProcessed
{
    public function __construct(public int $paymentId) {}
}
AThe event dispatch silently succeeds with a null paymentId.
BA TypeError is thrown indicating a missing argument for the constructor.
CA runtime exception occurs because the event class is abstract.
DThe event dispatch triggers but the listener receives an empty event object.
Attempts:
2 left
💡 Hint

Think about PHP constructor requirements when instantiating objects.