Download the PHP package artificertech/eloquent-attribute-middleware without Composer
On this page you can find all versions of the php package artificertech/eloquent-attribute-middleware. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download artificertech/eloquent-attribute-middleware
More information about artificertech/eloquent-attribute-middleware
Files in artificertech/eloquent-attribute-middleware
Package eloquent-attribute-middleware
Short Description :package_description
License MIT
Homepage https://github.com/artificertech/eloquent-attribute-middleware
Informations about the package eloquent-attribute-middleware
EloquentAttributeMiddleware
This package enables you to define middleware classes for your Eloquent model accessors and mutators using php 8 Attributes. This allows you to reuse complex code for your computed attributes. Take a look at contributing.md to see a to do list.
Requirements
php ^8.0, Laravel ^8
Installation
Via Composer
Usage
Accessors
create your accessor middleware class using laravel artisan
Configure your accessor middleware. Accessor middlware should modify the response of the $next() callback and return the modified value.
Add the middleware functionality to your Eloquent Model
Now any time you retrieve the name attribute it will be Uppercase
Execution Order
Accessors run in order of definition. In the following example the user 'name' attribute is stored in the database as 'Cole Shirley'
Execution order:
- the Upper __invoke method is called first which retrieves the value of the next callback
- the AppendTestString __invoke method is then called which retireves the value of the next callback
- the getNameAttribute method is called with the value 'Cole Shirley' from the stored model attributes
- that value is passed back to AppendTestString which then concatenates '_test' onto the value
- the modified string is passed back to Upper which makes the entire string uppercase
- the finalized string is passed back to the implemenation
Mutators
create your mutator middleware class using laravel artisan
Configure your mutator middleware. Mutator middlware should modify $value parameter before passing it to the $next() callback
Add the middleware functionality to your Eloquent Model
Now when you set the name attribute it will be lowercased
Execution Order
Mutators run in order of definition
Execution order:
- the Lower __invoke method is called first with the value 'Cole Shirley'
- the WithoutExtraWhitespace __invoke method is called with the value 'cole shirley'
- the setNameAttribute is called with the value 'cole shirley'
- if the setNameAttribute has a return value it is passed back to the implementation
Practical example: Caching model info from api
For most situations you should be able to use normal accessor and mutator functionality. However if you find yourself setting up complicated accessors or mutators repeatedly you may consider extracting that functionality into accessor and mutator middleware. A great example is if you want to cache data related to a model from an external api
Change log
Please see the changelog for more information on what has changed recently.
Testing
Contributing
Please see contributing.md for details and a todolist.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
- Cole Shirley
- All Contributors
License
MIT. Please see the license file for more information.