Skip to main content

Observer Pattern with Events/Listeners

Premium
4 min read

One of the most widely used design patterns in Laravel is Observer.

Actually, there are two Laravel features for this pattern:

The general idea is this:

  1. The "main" code is happening in an "active" way: in Controller, for example
  2. Then there are other "subscribed" classes observing what happened, waiting to be activated when needed

Event/Listener: Practical Example

Look at a typical Event/Listener example from Laravel Breeze:

app/Http/Controllers/Auth/RegisteredUserController.php:

use Illuminate\Auth\Events\Registered;
 
// ...
 
class RegisteredUserController extends Controller
{
public function store(Request $request): RedirectResponse
{
// ... validation
 
$user = User::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
 
event(new Registered($user));

The registration Controller fires the event Registered that can be listened to by others.

Inside that Registered Event class, which comes from Laravel core, we have $user as a property.

Illuminate/Auth/Events/Registered.php:

use Illuminate\Queue\SerializesModels;
 
class Registered
{
use SerializesModels;
 
public $user;
 
public function __construct($user)
{
$this->user = $user;
}
}

And then, for example, we want to create a listener to auto-create a Team for that new user.

Then, all we need to do:

  • Generate the Listener with php artisan make:listener CreateTeamListener
  • Pass the Registered Event as a parameter, and the Listener will auto-listen for that Event

app/Listeners/CreateTeamListener.php:

use App\Models\Team;
use Illuminate\Auth\Events\Registered;
 
// ...
 
class CreateTeamListener
{
public function handle(Registered $event): void
{
// Access the user using $event->user...
Team::create([
'name' => $event->user->name . "'s Team",
'user_id' => $event->user->id,
]);
}
}

With recent Laravel changes, we don't even need to register that Listener anywhere. The event discovery will take care of it.


Observer Example: The Same Pattern?

Eloquent Observers just happens to be a more convenient "wrapper" implementation of events/listeners.

For example...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (31 h 16 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

No comments yet…