In this article explain to example of laravel model observers. We will use laravel model observers. You can understand a concept of what is observers in laravel. This example will help you laravel model observers.
I will explain you how to use laravel model observers event, you can use with laravel multiple version. In Laravel, Observers allow you to listen to upon various events that happen to your models.
Key concept of Laravel Observers
- Observe Class: An observers is a class that groups event-handling methods for a specific model.
- Eloquent Events: Laravel models fire several events, and an observe can listen to these events:
created: Called after a record has been created.
updated: Called after a record is updated.
deleted: Called after a record is deleted.
restored: Called after a record is restored.
forceDeleted: Called record is deleted.
Steps to Use Laravel Model Observers
If you need to listen for multiple events on a model. You might want to group them together using an observer. Each of which received the affected model as an argument. Observers are created with an artisan command. We will explain all step below.
1. Creating an Observer:
You can generate an observe class using Artisan command.
php artisan make:observer UserObserver --model=User
This command creates an observer for the User model. Generated observer class will be placed in the App/Observes directory.
2. Defining Observer Methods:
In the observer class, you can define methods to the events. For example
<?php
namespace App\Observers;
use App\Models\User;
class UserObserver
{
/**
* Handle the User "created" event.
*
* @param \App\Models\User $user
* @return void
*/
public function created(User $user)
{
$user->email_verified_at = now();
$user->save();
}
/**
* Handle the User "updated" event.
*
* @param \App\Models\User $user
* @return void
*/
public function updated(User $user)
{
//
}
/**
* Handle the User "deleted" event.
*
* @param \App\Models\User $user
* @return void
*/
public function deleted(User $user)
{
//
}
/**
* Handle the User "restored" event.
*
* @param \App\Models\User $user
* @return void
*/
public function restored(User $user)
{
//
}
/**
* Handle the User "force deleted" event.
*
* @param \App\Models\User $user
* @return void
*/
public function forceDeleted(User $user)
{
//
}
}
3. Register the observer
In EventServiceProvider.php
<?php
namespace App\Providers;
use App\Models\User;
use App\Observers\UserObserver;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;
class EventServiceProvider extends ServiceProvider
{
/**
* The event to listener mappings for the application.
*
* @var array<class-string, array<int, class-string>>
*/
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
],
];
/**
* Register any events for your application.
*
* @return void
*/
public function boot()
{
User::observe(UserObserver::class);
}
/**
* Determine if events and listeners should be automatically discovered.
*
* @return bool
*/
public function shouldDiscoverEvents()
{
return false;
}
}
Advantage of Using Observers
- Separation of concerns: Observer allow you to keep your model and controller clean by handling the logic related to model event in a separated class.
- Reusability: You can easily reuse observer logic across multiple model and different parts of the application.
- Centralized logic: All the event-based logic for a model is located in a single observer, making it easy to manage.
Read also: Laravel get all models example
Conclusion
We hope that this helps to clarify the role of observers in Laravel. That have a better idea of when and how to use them. If you have repetitive tasks for a model, then you can use Laravel observers.
I hope this tutorial help you.