PHPackages                             alexstewartja/laravel-attribute-observer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. alexstewartja/laravel-attribute-observer

ActiveLibrary[Database &amp; ORM](/categories/database)

alexstewartja/laravel-attribute-observer
========================================

Observe (and react to) attribute changes made on Eloquent models.

1.7.8(2mo ago)7283.4k↓34.8%7[1 PRs](https://github.com/alexstewartja/laravel-attribute-observer/pulls)MITPHPPHP ^7.4 || ^8.0 || ^8.1 || ^8.2 || ^8.3 || ^8.4CI passing

Since Sep 30Pushed 2mo ago3 watchersCompare

[ Source](https://github.com/alexstewartja/laravel-attribute-observer)[ Packagist](https://packagist.org/packages/alexstewartja/laravel-attribute-observer)[ Docs](https://github.com/alexstewartja/laravel-attribute-observer)[ Fund](https://www.buymeacoffee.com/alexstewartja)[ GitHub Sponsors](https://github.com/alexstewartja)[ RSS](/packages/alexstewartja-laravel-attribute-observer/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (22)Used By (0)

Laravel Attribute Observer
==========================

[](#laravel-attribute-observer)

[![Laravel Attribute Observer - Social Image](https://camo.githubusercontent.com/5566feabb319736d903070d9b51496e180a3bb77a72e2cbef5f06b98328d63f2/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f4c61726176656c2532304174747269627574652532304f627365727665722e706e673f7468656d653d6c69676874267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d616c6578737465776172746a612532466c61726176656c2d6174747269627574652d6f62736572766572267061747465726e3d666c6f6174696e67436f6773267374796c653d7374796c655f32266465736372697074696f6e3d4f6273657276652b253238616e642b72656163742b746f2532392b6174747269627574652b6368616e6765732b6d6164652b6f6e2b456c6f7175656e742b6d6f64656c732e266d643d312673686f7757617465726d61726b3d3026666f6e7453697a653d313235707826696d616765733d68747470732533412532462532466c61726176656c2e636f6d253246696d672532466c6f676f6d61726b2e6d696e2e737667)](https://camo.githubusercontent.com/5566feabb319736d903070d9b51496e180a3bb77a72e2cbef5f06b98328d63f2/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f4c61726176656c2532304174747269627574652532304f627365727665722e706e673f7468656d653d6c69676874267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d616c6578737465776172746a612532466c61726176656c2d6174747269627574652d6f62736572766572267061747465726e3d666c6f6174696e67436f6773267374796c653d7374796c655f32266465736372697074696f6e3d4f6273657276652b253238616e642b72656163742b746f2532392b6174747269627574652b6368616e6765732b6d6164652b6f6e2b456c6f7175656e742b6d6f64656c732e266d643d312673686f7757617465726d61726b3d3026666f6e7453697a653d313235707826696d616765733d68747470732533412532462532466c61726176656c2e636f6d253246696d672532466c6f676f6d61726b2e6d696e2e737667)

[![Latest Stable Version](https://camo.githubusercontent.com/cfb4418a87292e3b8d9738103db04d847442bddb66b29f6c949e64bd9cdfe549/687474703a2f2f706f7365722e707567782e6f72672f616c6578737465776172746a612f6c61726176656c2d6174747269627574652d6f627365727665722f76)](https://packagist.org/packages/alexstewartja/laravel-attribute-observer)[![Total Downloads](https://camo.githubusercontent.com/e779bd7c7da5ef643a60aadb607ec3295f213c0f0c511c924aa394cd073d7525/687474703a2f2f706f7365722e707567782e6f72672f616c6578737465776172746a612f6c61726176656c2d6174747269627574652d6f627365727665722f646f776e6c6f616473)](https://packagist.org/packages/alexstewartja/laravel-attribute-observer)[![License](https://camo.githubusercontent.com/b03fa6dd0ad2c2e532e9dbd3458c57b77566d66a62891b4ce1739de217cc2822/687474703a2f2f706f7365722e707567782e6f72672f616c6578737465776172746a612f6c61726176656c2d6174747269627574652d6f627365727665722f6c6963656e7365)](https://packagist.org/packages/alexstewartja/laravel-attribute-observer)

[![GitHub Tests Action Status](https://camo.githubusercontent.com/2c73defa9c2f60b3bb9866a80b2feac70abe5c114036a7e1d3735ad7e61b26cc/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f616c6578737465776172746a612f6c61726176656c2d6174747269627574652d6f627365727665722f72756e2d74657374732e796d6c3f6c6162656c3d7465737473)](https://github.com/alexstewartja/laravel-attribute-observer/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/b559eff9babfe1cdcb3ca021fff5eea74b352c1f05533fb6b278c6e891732fba/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f616c6578737465776172746a612f6c61726176656c2d6174747269627574652d6f627365727665722f7068702d63732d66697865722e796d6c3f6c6162656c3d636f64652532307374796c65)](https://github.com/alexstewartja/laravel-attribute-observer/actions?query=workflow%3A%22Check+%26+fix+styling%22+branch%3Amain)

[![PHP Versions Supported](https://camo.githubusercontent.com/00d84828fdc82799f98da60809c9c2ce9f760b10226938262fe821c211d5310a/687474703a2f2f706f7365722e707567782e6f72672f616c6578737465776172746a612f6c61726176656c2d6174747269627574652d6f627365727665722f726571756972652f706870)](https://packagist.org/packages/alexstewartja/laravel-attribute-observer)[![Laravel Versions Supported](https://camo.githubusercontent.com/08d469465463ef41f40a22b9419df6e748738e0ec39fabc6888eeee234daf4e5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f616c6578737465776172746a612f6c61726176656c2d6174747269627574652d6f627365727665722f696c6c756d696e6174652f636f6e7472616374733f6c6162656c3d6c61726176656c)](https://packagist.org/packages/alexstewartja/laravel-attribute-observer)

[![Buy Me A Coffee](https://camo.githubusercontent.com/3ce5cc1c728bf8e0be5bbd132e9d5413d3a922cecfc3381decdcc82c5f66da9b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4275795f4d652d415f436f666665652d6f72616e67653f6c6f676f3d6275792d6d652d612d636f66666565)](https://buymeacoffee.com/alexstewartja)

A Laravel package which allows you to observe (and react to) attribute changes made on Eloquent models.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require alexstewartja/laravel-attribute-observer
```

Configuration
-------------

[](#configuration)

Publish the config file (`config/attribute-observer.php`) with:

```
php artisan vendor:publish --provider="AlexStewartJA\LaravelAttributeObserver\LaravelAttributeObserverServiceProvider"
```

This is the default content of the published config file:

```
return [
    /*
    |--------------------------------------------------------------------------
    | Attribute Observers
    |--------------------------------------------------------------------------
    |
    | Here you may configure all desired Models and their respective Attribute
    | Observers. For example:
    |
    | 'observers' => [
    |    \App\Models\Order::class => [
    |        \App\AttributeObservers\OrderStatusObserver::class,
    |    ],
    | ]
    |
    */

    'observers' => [
        // Define your model & attribute observers here...
    ]
];
```

Populate the `observers` array with your desired **Model =&gt; Attribute Observer** mappings.

Usage
-----

[](#usage)

### Attribute Observers

[](#attribute-observers)

The `make:laravel-attribute-observer` Artisan command is the easiest way to create a new attribute observer class:

```
php artisan make:laravel-attribute-observer OrderStatusObserver --model=Order
```

This command will place the new attribute observer in your `App/AttributeObservers` directory. If this directory does not exist, Artisan will create it for you. Your freshly generated attribute observer will look like the following:

```
