Vivek Mistry 👋

I’m a Certified Senior Laravel Developer with 6+ years of experience , specializing in building robust APIs and admin panels, frontend templates converting them into fully functional web applications.

Book A Call
  • 29 Jul, 2025
  • 321 Views
  • Laravel Tip: Use Model Observers to Keep Controllers Clean

Model Observers

Laravel Tip: Use Model Observers to Keep Controllers Clean

Ever find yourself sprinkling the same “after save” or “before delete” logic in multiple controllers?

Laravel Observers let you move that into a single, reusable place.

Before (Logic Scattered Everywhere)

// In Controller
$user = User::create($data);
Mail::to($user->email)->send(new WelcomeMail($user));

After (With Observer)

Create an Observer

php artisan make:observer UserObserver --model=User

Add Your Logic

class UserObserver
{
    public function created(User $user)
    {
        Mail::to($user->email)->send(new WelcomeMail($user));
    }
}

Register It (in AppServiceProvider)

use App\Models\User;
use App\Observers\UserObserver;
public function boot()
{
    User::observe(UserObserver::class);
}

Now every time a user is created (no matter from where), the welcome email fires automatically.

Why It’s Awesome

  • Keeps controllers lightweight
  • Avoids duplicating logic
  • Centralizes “model lifecycle” actions


Share: