Laravel Observers vs Model Events: Side Effects Done Right | Mohamed Said       [Skip to content](#main)  [ ![](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png) Mohamed SaidLaravel Backend Engineer ](https://www.msaied.com/public) - [Home](https://www.msaied.com/public)
- [Projects](https://www.msaied.com/public/projects)
- [Articles](https://www.msaied.com/public/articles)
- [Certificates](https://www.msaied.com/public/certificates)
- [About](https://www.msaied.com/public#about)

           [  Contact](https://www.msaied.com/public#contact) Menu 

Menu
----

Close 

 - [HomeStart here](https://www.msaied.com/public)
- [ProjectsCase studies](https://www.msaied.com/public/projects)
- [ArticlesEngineering notes](https://www.msaied.com/public/articles)
- [CertificatesCredentials](https://www.msaied.com/public/certificates)
- [AboutHow I work](https://www.msaied.com/public#about)
- [ContactGet in touch](https://www.msaied.com/public#contact)

  [Start a conversation](https://www.msaied.com/public#contact) [WhatsApp](https://wa.me/201094619204) [Email](mailto:hello@msaied.com) 

 1. [Home](https://www.msaied.com/public)
2. /
3. [Articles](https://www.msaied.com/public/articles)
4. /
5. Laravel Observers vs. Model Events: Choosing the Right Hook for Side Effects

 Laravel Observers vs. Model Events: Choosing the Right Hook for Side Effects
=============================================================================

 Model events and observers both react to Eloquent lifecycle moments, but picking the wrong one creates hidden coupling and untestable code. Here is a practical guide to choosing correctly.

 ![](https://cdn.msaied.com/01M22N44A70A5MC2S599JP0MPH.webp) [Mohamed Said](https://www.msaied.com/public#person) Published 11 Jul 2026 · Updated 11 Jul 2026 · 3 min read

ShareCopy linkCopied

 ![Laravel Observers vs. Model Events: Choosing the Right Hook for Side Effects](https://cdn.msaied.com/413/ec9d48bb1167db4a2ec2e0784f3be002.png) 

  On this page +1. [The Problem With Inline Model Events](#the-problem-with-inline-model-events)
2. [What Observers Actually Buy You](#what-observers-actually-buy-you)
3. [When to Stick With Inline Events](#when-to-stick-with-inline-events)
4. [Testing: Faking Observers Without Touching the Database](#testing-faking-observers-without-touching-the-database)
5. [Avoiding the Observer Bloat Trap](#avoiding-the-observer-bloat-trap)
6. [Takeaways](#takeaways)

 The Problem With Inline Model Events
------------------------------------

Eloquent ships with a rich lifecycle: `creating`, `created`, `updating`, `updated`, `saving`, `saved`, `deleting`, `deleted`, and more. The quickest way to hook into them is directly inside the model:

```php
// App\Models\Order.php
protected static function booted(): void
{
    static::created(function (Order $order) {
        SendOrderConfirmationEmail::dispatch($order);
        InventoryService::reserve($order);
        AuditLog::record('order.created', $order->id);
    });
}

```

This works — until it doesn't. Three side effects buried in `booted()` make the model hard to read, impossible to disable in tests without hacks, and a magnet for more logic over time.

What Observers Actually Buy You
-------------------------------

An observer moves each event into a named method on a dedicated class, giving you a single place to reason about lifecycle reactions:

```php
// App\Observers\OrderObserver.php
final class OrderObserver
{
    public function __construct(
        private readonly AuditLogger $logger,
    ) {}

    public function created(Order $order): void
    {
        SendOrderConfirmationEmail::dispatch($order);
        $this->logger->record('order.created', $order->id);
    }

    public function deleted(Order $order): void
    {
        $this->logger->record('order.deleted', $order->id);
    }
}

```

Register it in a service provider (or via `#[ObservedBy]` in Laravel 10+):

```php
// Using the attribute — no service provider registration needed
#[ObservedBy(OrderObserver::class)]
class Order extends Model {}

```

Because the observer is resolved through the container, constructor injection works out of the box. That alone is worth the switch from closures.

When to Stick With Inline Events
--------------------------------

Observers are not always the right tool:

- **Simple, single-purpose hooks** — setting a UUID or slug on `creating` belongs in `booted()`. It is model-internal behaviour, not a cross-cutting side effect.
- **Package models you do not own** — you cannot add `#[ObservedBy]` to a vendor class; register via `Model::observe()` in a service provider instead.
- **Conditional registration** — if the hook only applies in certain contexts (e.g., a specific tenant feature flag), a closure in a service provider is clearer than an observer that checks a flag on every event.

```php
// Good: model-internal concern stays in booted()
protected static function booted(): void
{
    static::creating(function (Order $order) {
        $order->uuid ??= (string) Str::uuid();
    });
}

```

Testing: Faking Observers Without Touching the Database
-------------------------------------------------------

The biggest win observers give you is testability. Pest makes it trivial:

```php
use App\Observers\OrderObserver;
use App\Models\Order;

it('dispatches confirmation email after order creation', function () {
    Mail::fake();

    $order = Order::factory()->create();

    Mail::assertQueued(OrderConfirmationMail::class, fn ($mail) =>
        $mail->order->is($order)
    );
});

```

Need to suppress the observer entirely for a test that does not care about side effects?

```php
it('calculates totals correctly', function () {
    Order::withoutObservers(function () {
        $order = Order::factory()->create(['subtotal' => 100]);
        expect($order->total)->toBe(110); // tax applied via cast
    });
});

```

`withoutObservers` is a first-class Laravel API — no monkey-patching required.

Avoiding the Observer Bloat Trap
--------------------------------

Observers can become a second model if you are not careful. Keep them thin:

- **Dispatch jobs, do not execute work.** The observer fires synchronously inside the request cycle. Heavy logic belongs in a queued job.
- **One observer per model.** Multiple observers on the same model fire in registration order — a subtle source of bugs.
- **No cross-model writes.** An observer that saves a related model triggers *that* model's observers, creating hard-to-trace chains. Use a dedicated action or service instead.

Takeaways
---------

- Use `booted()` closures for model-internal concerns (default values, derived attributes).
- Use observers for cross-cutting side effects that benefit from DI and named methods.
- Prefer `#[ObservedBy]` in Laravel 10+ to keep registration co-located with the model.
- Keep observers as dispatchers, not executors — push real work into queued jobs.
- Use `Model::withoutObservers()` in tests to isolate the unit under test cleanly.

- [laravel](https://www.msaied.com/public/articles?search=laravel)
- [eloquent](https://www.msaied.com/public/articles?search=eloquent)
- [observers](https://www.msaied.com/public/articles?search=observers)
- [testing](https://www.msaied.com/public/articles?search=testing)
- [architecture](https://www.msaied.com/public/articles?search=architecture)

 Frequently asked questions 
---------------------------

  Does using `#\[ObservedBy\]` affect performance compared to registering in a service provider?No meaningful difference. The attribute is read once during boot and the observer is registered identically to the service provider approach. Choose based on readability — the attribute keeps registration close to the model.

   Can I have multiple observers on the same Eloquent model?Yes, but they fire in registration order and there is no built-in priority mechanism. Multiple observers on one model often signal that the model has too many responsibilities. Consider consolidating into one observer or extracting domain events.

   Will observers fire when using `Model::query()-&gt;update()` or bulk inserts?No. Mass updates and bulk inserts bypass Eloquent model hydration entirely, so no lifecycle events — and therefore no observers — are triggered. If you need hooks on bulk operations, dispatch an explicit event or job after the query.

   ![Mohamed Said](https://cdn.msaied.com/01M22N44A70A5MC2S599JP0MPH.webp)About the author
----------------

[Mohamed Said](https://www.msaied.com/public#person)Senior Backend Engineer specializing in Laravel, scalable SaaS platforms, APIs, and cloud infrastructure. I build secure, high-performance web applications that help businesses grow.

[About](https://www.msaied.com/public#about) [GitHub ↗](https://github.com/EG-Mohamed) [LinkedIn ↗](https://www.linkedin.com/in/msaiedm/) [WhatsApp ↗](https://wa.me/201094619204) [Email Address ↗](mailto:hello@msaied.com) [My CV ↗](https://drive.google.com/file/u/0/d/1MF20IPRJyzfy32mhEutjL5EpSls0w2Q8/view)  

   [Previous articleFilament v3 Table Bulk Actions: Custom Confirmation, Progress Feedback, and Job Dispatch](https://www.msaied.com/public/articles/filament-v3-table-bulk-actions-custom-confirmation-progress-feedback-and-job-dispatch) [Next articleJob Batching, Chaining, and Rate-Limited Middleware for Laravel Queues](https://www.msaied.com/public/articles/job-batching-chaining-and-rate-limited-middleware-for-laravel-queues)  

   On this page
-------------

1. [The Problem With Inline Model Events](#the-problem-with-inline-model-events)
2. [What Observers Actually Buy You](#what-observers-actually-buy-you)
3. [When to Stick With Inline Events](#when-to-stick-with-inline-events)
4. [Testing: Faking Observers Without Touching the Database](#testing-faking-observers-without-touching-the-database)
5. [Avoiding the Observer Bloat Trap](#avoiding-the-observer-bloat-trap)
6. [Takeaways](#takeaways)

 ###  Have a technical challenge?

 Tell me what you’re building. I reply within two working days.

[Start a conversation](https://www.msaied.com/public#contact) 

   Related articles
-----------------

 [ ![](https://cdn.msaied.com/740/cce86edc21eddcbdd2f2454fadaf9c70.png)  · 3 min read### The Pipeline Pattern in Laravel: Custom Pipelines Beyond Middleware

5 Oct 2026 ](https://www.msaied.com/public/articles/the-pipeline-pattern-in-laravel-custom-pipelines-beyond-middleware-1) [ ![](https://cdn.msaied.com/739/2d6897fdcdcf090613f96f72a64b8a78.png)  · 4 min read### MySQL Full-Text Search in Laravel: Indexes, Relevance Scoring, and Boolean Mode

4 Oct 2026 ](https://www.msaied.com/public/articles/mysql-full-text-search-in-laravel-indexes-relevance-scoring-and-boolean-mode) [ ![](https://cdn.msaied.com/738/073696a3fefe18bec825beec5ac658f5.png)  · 4 min read### Laravel Queue Rate-Limited Middleware: Throttling Jobs Without Losing Work

4 Oct 2026 ](https://www.msaied.com/public/articles/laravel-queue-rate-limited-middleware-throttling-jobs-without-losing-work) 

  Have a technical challenge?
----------------------------

Tell me what you’re building. I reply within two working days.

 [Discuss your project ↗](https://www.msaied.com/public#contact) 

  © 2026 Mohamed Said · Built with Laravel, meant to last.Senior Backend Engineer specializing in Laravel, scalable SaaS platforms, APIs, and cloud infrastructure. I build secure, high-performance web applications that help businesses grow.

 - [Home](https://www.msaied.com/public)
- [Articles](https://www.msaied.com/public/articles)
- [Certificates](https://www.msaied.com/public/certificates)
- [GitHub](https://github.com/EG-Mohamed)
- [LinkedIn](https://www.linkedin.com/in/msaiedm/)
- [WhatsApp](https://wa.me/201094619204)
- [Email Address](mailto:hello@msaied.com)
- [My CV](https://drive.google.com/file/u/0/d/1MF20IPRJyzfy32mhEutjL5EpSls0w2Q8/view)
- [Sitemap](https://www.msaied.com/public/sitemap.xml)
