Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Defining Events in Laravel
📖 Scenario: You are building a simple Laravel application that needs to notify when a new user registers. You will define an event to represent this action.
🎯 Goal: Create a Laravel event class called UserRegistered that holds the user data. This event will be used to signal when a user registers.
📋 What You'll Learn
Create an event class named UserRegistered
Add a public property $user to hold the user data
Create a constructor that accepts a $user parameter and assigns it to the property
Ensure the event class uses the Illuminate\Foundation\Events\Dispatchable and Illuminate\Queue\SerializesModels traits
💡 Why This Matters
🌍 Real World
Events in Laravel help you separate concerns by signaling when something important happens, like a user registering, so other parts of your app can react without being tightly connected.
💼 Career
Understanding how to define and use events is key for Laravel developers to build scalable and maintainable applications that follow modern design patterns.
Progress0 / 4 steps
1
Create the UserRegistered event class
Create a PHP class named UserRegistered inside the app/Events directory. Start the class with the namespace App\Events and declare the class without any properties or methods yet.
Laravel
Hint
Use namespace App\Events; and declare class UserRegistered.
2
Add the user property and constructor
Inside the UserRegistered class, add a public property named $user. Then create a constructor method __construct that accepts a parameter $user and assigns it to the $user property.
Laravel
Hint
Define public $user; and a constructor that sets $this->user = $user;.
3
Use Dispatchable and SerializesModels traits
Add the use statements for the traits Illuminate\Foundation\Events\Dispatchable and Illuminate\Queue\SerializesModels at the top of the file. Then inside the UserRegistered class, use these traits with the use keyword.
Laravel
Hint
Import the traits and add use Dispatchable, SerializesModels; inside the class.
4
Complete the event class
Ensure the UserRegistered event class is complete with namespace, trait imports, trait usage, public $user property, and constructor. The class should be ready to be used in your Laravel application.
Laravel
Hint
Review the full class to confirm all parts are included.
Practice
(1/5)
1. What is the main purpose of defining events in a Laravel application?
easy
A. To respond to actions in a clean and organized way
B. To directly modify database tables
C. To create user interface components
D. To handle HTTP routing
Solution
Step 1: Understand the role of events
Events in Laravel are designed to help the app respond to actions or changes without mixing code logic.
Step 2: Compare with other options
Modifying database tables, creating UI components, and handling routing are done by other parts of Laravel, not events.
Final Answer:
To respond to actions in a clean and organized way -> Option A
Quick Check:
Events = Respond to actions [OK]
Hint: Events handle actions, not UI or database directly [OK]
Common Mistakes:
Confusing events with database migrations
Thinking events create UI elements
Mixing events with routing logic
2. Which artisan command correctly creates a new event named UserRegistered?
easy
A. php artisan make:event UserRegistered
B. php artisan event:create UserRegistered
C. php artisan make:event --new UserRegistered
D. php artisan create:event UserRegistered
Solution
Step 1: Recall the artisan command for events
The correct command to create an event is php artisan make:event EventName.
Step 2: Check each option
Only php artisan make:event UserRegistered matches the correct syntax. Others use invalid commands or flags.
Final Answer:
php artisan make:event UserRegistered -> Option A
Quick Check:
make:event = create event [OK]
Hint: Use 'make:event' with artisan to create events [OK]
Common Mistakes:
Using 'event:create' instead of 'make:event'
Adding unnecessary flags like '--new'
Swapping command order
3. Given this event class snippet:
class OrderShipped {
public $order;
public function __construct($order) {
$this->order = $order;
}
}
What will be the correct way to fire this event with an $order object?
medium
A. event(OrderShipped($order));
B. OrderShipped::fire($order);
C. event(new OrderShipped($order));
D. fire_event('OrderShipped', $order);
Solution
Step 1: Understand how to fire events in Laravel
Events are fired by calling the global event() helper with a new instance of the event class.
Step 2: Check each option
event(new OrderShipped($order)); correctly uses event(new OrderShipped($order)). event(OrderShipped($order)); misses new, C and D use invalid syntax.
Final Answer:
event(new OrderShipped($order)); -> Option C
Quick Check:
Fire event = event(new EventClass) [OK]
Hint: Always use 'new' keyword inside event() to fire events [OK]
Common Mistakes:
Omitting 'new' keyword when firing event
Trying to call static fire methods
Using non-existent helper functions
4. Identify the error in this event class definition:
class ProductCreated {
public $product;
public function ProductCreated($product) {
$this->product = $product;
}
}
medium
A. Property should be private, not public
B. Missing public keyword before property
C. Event class must extend a base Event class
D. Constructor method name should be __construct, not class name
Solution
Step 1: Check constructor syntax in PHP
In modern PHP (and Laravel), constructors must be named __construct, not the class name.
Step 2: Verify other options
Property visibility is correct as public, extending base class is optional, so only constructor name is wrong.
Final Answer:
Constructor method name should be __construct, not class name -> Option D
Quick Check:
Constructor = __construct method [OK]
Hint: Use __construct() for constructors, not class name [OK]
Common Mistakes:
Using old PHP constructor style
Forgetting visibility keywords
Assuming event classes must extend a base class
5. You want to define an event InvoicePaid that carries an Invoice object and a payment amount. Which is the best way to define its constructor?
hard
A. public function __construct(array $data) { $this->invoice = $data['invoice']; $this->amount = $data['amount']; }
B. public function __construct(Invoice $invoice, float $amount) { $this->invoice = $invoice; $this->amount = $amount; }
C. public function InvoicePaid(Invoice $invoice, float $amount) { $this->invoice = $invoice; $this->amount = $amount; }
D. public function __construct($invoice, $amount) { $invoice = $invoice; $amount = $amount; }
Solution
Step 1: Check constructor naming and parameter types
The constructor must be named __construct and accept typed parameters for clarity and safety.
Step 2: Verify property assignments
public function __construct(Invoice $invoice, float $amount) { $this->invoice = $invoice; $this->amount = $amount; } correctly assigns parameters to properties. public function __construct($invoice, $amount) { $invoice = $invoice; $amount = $amount; } assigns parameters to themselves (wrong). public function InvoicePaid(Invoice $invoice, float $amount) { $this->invoice = $invoice; $this->amount = $amount; } uses old constructor name. public function __construct(array $data) { $this->invoice = $data['invoice']; $this->amount = $data['amount']; } uses array but less clear and no type safety.
Final Answer:
public function __construct(Invoice $invoice, float $amount) { $this->invoice = $invoice; $this->amount = $amount; } -> Option B
Quick Check:
Use __construct with typed params and assign to properties [OK]
Hint: Use __construct with typed params and assign to $this->props [OK]