When building applications with Laravel 12, managing repetitive model logic can quickly become messy if it’s placed directly inside controllers or models. That’s where Observers come in. Observers allow you to listen to model events and handle specific actions when those events are triggered.
In this article, we’ll explore what Observers are, why they’re useful, and how you can implement them in a Laravel 12 project.
🔹 What is an Observer?
An Observer in Laravel is a class that groups multiple model event listeners into a single class. Laravel models fire various events such as:
-
creating – Before a record is created.
-
created – After a record is created.
-
updating – Before a record is updated.
-
updated – After a record is updated.
-
saving – Before a record is saved (both creating and updating).
-
saved – After a record is saved.
-
deleting – Before a record is deleted.
-
deleted – After a record is deleted.
-
restoring – Before a soft-deleted record is restored.
-
restored – After a soft-deleted record is restored.
Instead of writing these event hooks directly inside the model, Observers help organize them in a separate class.
🔹 Why Use Observers?
-
Clean Code: Keeps your models lean and focused only on relationships and attributes.
-
Reusability: Centralizes logic for a specific model, making it easier to maintain.
-
Separation of Concerns: Moves business logic out of controllers and models.
-
Consistency: Ensures that important actions (like logging or sending notifications) are always applied when model events occur.
🔹 Creating an Observer in Laravel 12
Laravel provides an artisan command to create an observer:
php artisan make:observer UserObserver --model=User
This will generate a new observer class in app/Observers/UserObserver.php and link it to the User model.
🔹 Example: User Observer
Imagine you want to automatically send a welcome email when a user registers. Instead of putting that logic inside the controller, you can handle it inside the UserObserver.
app/Observers/UserObserver.php
<?php
namespace App\Observers;
use App\Models\User;
use App\Mail\WelcomeEmail;
use Illuminate\Support\Facades\Mail;
class UserObserver
{
/**
* Handle the User "created" event.
*/
public function created(User $user): void
{
// Send a welcome email
Mail::to($user->email)->send(new WelcomeEmail($user));
}
/**
* Handle the User "updated" event.
*/
public function updated(User $user): void
{
// Example: log user profile updates
\Log::info("User {$user->id} was updated.");
}
/**
* Handle the User "deleted" event.
*/
public function deleted(User $user): void
{
// Example: clean up related records
\Log::warning("User {$user->id} was deleted.");
}
}
🔹 Registering the Observer
To make Laravel use the observer, register it inside the AppServiceProvider or create a dedicated EventServiceProvider.
app/Providers/AppServiceProvider.php
<?php
namespace App\Providers;
use App\Models\User;
use App\Observers\UserObserver;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap services.
*/
public function boot(): void
{
User::observe(UserObserver::class);
}
}
Now, whenever a User model is created, updated, or deleted, the corresponding methods in the UserObserver will be triggered automatically.
🔹 Best Practices for Using Observers
-
Keep Observers Focused: Each observer should handle logic for one model only.
-
Avoid Heavy Logic: Don’t put too much business logic in observers; delegate tasks to services when needed.
-
Use for Cross-Cutting Concerns: Perfect for actions like logging, sending notifications, updating audit trails, and syncing external systems.
-
Naming Convention: Always suffix observer names with Observer (e.g., UserObserver, OrderObserver).
🔹 Real-World Use Cases
-
Automatically generating slugs when posts are created.
-
Sending emails or notifications on user actions.
-
Maintaining audit logs when models are updated or deleted.
-
Cleaning up related records when a parent model is deleted.
-
Updating caches after a model change.
✅ Conclusion
Observers in Laravel 12 provide a clean and structured way to handle model events. By separating this logic into dedicated classes, you make your codebase more maintainable, testable, and easier to understand.
If your project requires executing tasks whenever models change, consider leveraging Observers—they’re one of Laravel’s most elegant features.