https://github.com/alexstewartja/laravel-attribute-observer
Observe (and react to) attribute changes made on Eloquent models.
https://github.com/alexstewartja/laravel-attribute-observer
attribute eloquent events laravel observer
Last synced: about 1 month ago
JSON representation
Observe (and react to) attribute changes made on Eloquent models.
- Host: GitHub
- URL: https://github.com/alexstewartja/laravel-attribute-observer
- Owner: alexstewartja
- License: mit
- Created: 2021-09-30T02:31:29.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-03-14T16:54:10.000Z (2 months ago)
- Last Synced: 2025-04-02T10:48:23.327Z (about 1 month ago)
- Topics: attribute, eloquent, events, laravel, observer
- Language: PHP
- Homepage:
- Size: 44.9 KB
- Stars: 73
- Watchers: 3
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
# Laravel Attribute Observer

[](https://packagist.org/packages/alexstewartja/laravel-attribute-observer)
[](https://packagist.org/packages/alexstewartja/laravel-attribute-observer)
[](https://packagist.org/packages/alexstewartja/laravel-attribute-observer)[](https://github.com/alexstewartja/laravel-attribute-observer/actions?query=workflow%3Arun-tests+branch%3Amain)
[](https://github.com/alexstewartja/laravel-attribute-observer/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain)[](https://packagist.org/packages/alexstewartja/laravel-attribute-observer)
[](https://packagist.org/packages/alexstewartja/laravel-attribute-observer)[](https://buymeacoffee.com/alexstewartja)
A Laravel package which allows you to observe (and react to) attribute changes made on Eloquent models.
## Installation
You can install the package via composer:
```bash
composer require alexstewartja/laravel-attribute-observer
```## Configuration
Publish the config file (`config/attribute-observer.php`) with:
```bash
php artisan vendor:publish --provider="AlexStewartJA\LaravelAttributeObserver\LaravelAttributeObserverServiceProvider"
```This is the default content of the published config file:
```php
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 => Attribute Observer** mappings.
## Usage
### Attribute Observers
The `make:laravel-attribute-observer` Artisan command is the easiest way to create a new attribute observer class:
```bash
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:```php
_Attribute Observer methods are always supplied with the **model instance**, the **new attribute value** and the **previous attribute
value**, in that order._### Events
You may observe all the typical CRUD events dispatched by Eloquent models during their lifecycles. Supported events are:
`creating`, `created`, `updating`, `updated`, `saving`, `saved`, `deleting`, `deleted`.The naming convention to follow when defining attribute observer methods is: **on|AttributeName|Event**
So, let's say we want to check a user's email against a global spam/vanity mail list each time they attempt to update it,
we would implement that logic in an attribute observer method called `onEmailUpdating`.> _Note that the attribute name must be in **[PascalCase](https://techterms.com/definition/pascalcase)** and the
> event name must be **[Capitalized](https://wikipedia.org/wiki/Capitalization)**._## Testing
```bash
composer test
```## Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Contributing
Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.
## Security Vulnerabilities
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
## Credits
- [Alex Stewart](https://github.com/alexstewartja)
- [All Contributors](../../contributors)## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
## Donations
I maintain this package in my spare time.
If it's beneficial to you, consider buying me a coffee to keep it improving.[](https://buymeacoffee.com/alexstewartja)