---
title: "Observers — Options for Laravel | Roundly"
description: "React to one specific option changing — closures or invokable classes, fired on set and forget in registration order."
url: https://roundly-consulting.com/open-source/docs/options-for-laravel/observers
language: en
---

[All packages](https://roundly-consulting.com/open-source.md)

[Options for Laravel](https://roundly-consulting.com/open-source/docs/options-for-laravel.md)

# Observers

Observers react to one specific option changing, without a global listener that filters by key. Register them in a service provider’s boot():

```php
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
use RoundlyConsulting\Options\DataTransferObjects\OptionChange;
use RoundlyConsulting\Options\Enums\OptionChangeType;
use RoundlyConsulting\Options\Facades\Options;

Options::observe(ThemeOption::class, function (mixed $value, ?Model $owner, OptionChange $change): void {
    // $change->type is OptionChangeType::Set or ::Forgotten
    Cache::forget('compiled-theme');
});

// Invokable class-string (resolved from the container), registered by key:
Options::observe('theme', RecompileThemeListener::class);

Options::forgetObservers(ThemeOption::class); // drop one option’s observers
Options::flushObservers();                    // drop them all
```

## Invokable observers

A class-string observer is resolved from the container on each change, so constructor injection works:

```php
use Illuminate\Database\Eloquent\Model;
use RoundlyConsulting\Options\DataTransferObjects\OptionChange;

final class RecompileThemeListener
{
    public function __construct(private ThemeCompiler $compiler) {}

    public function __invoke(mixed $value, ?Model $owner, OptionChange $change): void
    {
        $this->compiler->compileFor($owner);
    }
}
```

A class-string without \_\_invoke throws InvalidOptionObserver when you register it.

## The change payload

Every observer receives (mixed $value, ?Model $owner, OptionChange $change):

| Property | Type | Meaning |
| --- | --- | --- |
| `key` | string | The option’s key(). |
| `optionClass` | class-string | The option class that changed. |
| `type` | OptionChangeType | OptionChangeType::Set or OptionChangeType::Forgotten. |
| `value` | mixed | The new value, cast — the same value get() returns. null for a forgotten option. |
| `owner` | ?Model | The scope that changed — null for the global scope. |

## Rules

- Observers fire on set() and forget(), in registration order — never on reads.
- Registering by class-string or by registry key targets the same option.
- The write calls them itself — they don’t hang off OptionSet / OptionForgotten — so they run with options.events.enabled off, under an Event::fake() of those events and under Options::fake().
- Like the events, they run once the write is durable — after the outermost commit inside a transaction, and never for a write that rolls back.

## Show your open-source love

This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.

[More ways to support, including crypto](https://roundly-consulting.com/support-us.md)

By donating, you agree to our [donation terms](https://roundly-consulting.com/donation-terms.md).

[Support our open source work (opens in a new tab)](https://donate.stripe.com/dRmeVe8FX5PF1Qd9pXcEw00) [Join us on Patreon (opens in a new tab)](https://www.patreon.com/cw/roundly)

## Want this built into your product?

We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.

[Get a quote in 48 hours](https://roundly-consulting.com/contact.md) [Browse all packages](https://roundly-consulting.com/open-source/docs/options-for-laravel.md)
