0
0
Laravelframework~20 mins

Defining events in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Laravel Events Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when this Laravel event is fired?

Consider this Laravel event class and listener setup. What will be the output when the event is fired?

Laravel
namespace App\Events;

use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class UserRegistered
{
    use Dispatchable, SerializesModels;

    public $username;

    public function __construct($username)
    {
        $this->username = $username;
    }
}

// Listener
namespace App\Listeners;

class SendWelcomeEmail
{
    public function handle($event)
    {
        echo "Welcome email sent to {$event->username}";
    }
}

// Somewhere in controller or service
use App\Events\UserRegistered;

event(new UserRegistered('Alice'));
AWelcome email sent to Alice
BError: Undefined property username
CNo output because listener is not registered
DWelcome email sent to
Attempts:
2 left
💡 Hint

Think about whether the listener is automatically called just by firing the event.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a Laravel event class with a public property?

Choose the correct Laravel event class definition that includes a public property $data and uses the recommended traits.

A
class DataUpdated {
    use Dispatchable, SerializesModels;
    public $data;
    public function __construct($data) {
        $this->data = $data;
    }
}
B
class DataUpdated {
    use Dispatchable, SerializesModels
    public $data;
    function __construct($data) {
        $this->data = $data;
    }
}
C
class DataUpdated {
    public $data;
    public function __construct($data) {
        $this->data = $data;
    }
}
D
class DataUpdated {
    use Dispatchable, SerializesModels;
    private $data;
    public function __construct($data) {
        $this->data = $data;
    }
}
Attempts:
2 left
💡 Hint

Remember to include semicolons after traits and use public visibility for properties to be accessible.

state_output
advanced
2:00remaining
What is the value of $event->count after firing this event multiple times?

Given this Laravel event class, what will be the value of $event->count after firing the event three times with counts 1, 2, and 3?

Laravel
namespace App\Events;

use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class CounterUpdated
{
    use Dispatchable, SerializesModels;

    public $count;

    public function __construct($count)
    {
        $this->count = $count;
    }
}

// Firing events
$event1 = new CounterUpdated(1);
event($event1);
$event2 = new CounterUpdated(2);
event($event2);
$event3 = new CounterUpdated(3);
event($event3);

// What is $event3->count?
A1
Bnull
C2
D3
Attempts:
2 left
💡 Hint

Each event instance holds its own data passed in constructor.

🔧 Debug
advanced
2:00remaining
Why does this Laravel event listener not receive the event data?

Examine the event and listener code below. Why does the listener not print the username when the event is fired?

Laravel
// Event
namespace App\Events;

use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class UserLoggedIn
{
    use Dispatchable, SerializesModels;

    public $username;

    public function __construct($username)
    {
        $this->username = $username;
    }
}

// Listener
namespace App\Listeners;

class LogLogin
{
    public function handle()
    {
        echo "User logged in: " . $this->username;
    }
}

// Firing event
event(new \App\Events\UserLoggedIn('Bob'));
AListener handle method lacks the event parameter to receive event data
BEvent class is missing the SerializesModels trait
CEvent is not fired correctly with event() helper
DListener class is missing a constructor
Attempts:
2 left
💡 Hint

Check the listener's handle method signature.

🧠 Conceptual
expert
2:00remaining
Which statement best describes Laravel event broadcasting?

Choose the statement that correctly explains Laravel event broadcasting.

ABroadcasting queues events to run only after database transactions commit
BBroadcasting sends events to client-side JavaScript in real-time using channels and websockets
CBroadcasting automatically logs all events to Laravel log files
DBroadcasting delays event listeners to run asynchronously on the server
Attempts:
2 left
💡 Hint

Think about how Laravel sends events beyond the server to the browser.