Download the PHP package alexstewartja/laravel-attribute-observer without Composer
On this page you can find all versions of the php package alexstewartja/laravel-attribute-observer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download alexstewartja/laravel-attribute-observer
More information about alexstewartja/laravel-attribute-observer
Files in alexstewartja/laravel-attribute-observer
Package laravel-attribute-observer
Short Description Observe (and react to) attribute changes made on Eloquent models.
License MIT
Homepage https://github.com/alexstewartja/laravel-attribute-observer
Informations about the package laravel-attribute-observer
Laravel Attribute Observer
Requirements
- PHP: 7.4+
- Laravel: 7+
Installation
You can install the package via composer:
Configuration
Publish the config file (config/attribute-observer.php
) with:
This is the default content of the published config file:
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:
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:
You may then modify the code to match your business logic, for example:
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 and the event name must be Capitalized.
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Alex Stewart
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
Donations
I maintain this package in my spare time. If it's beneficial to you, consider donating or buying me a coffee to keep it improving.
All versions of laravel-attribute-observer with dependencies
spatie/laravel-package-tools Version ~1.12.1|^1.13.0
illuminate/contracts Version ^7.0|^8.0|^9.0|^10.0|^11.0