Download the PHP package mindtwo/laravel-dynamic-model-mutators without Composer
On this page you can find all versions of the php package mindtwo/laravel-dynamic-model-mutators. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mindtwo/laravel-dynamic-model-mutators
More information about mindtwo/laravel-dynamic-model-mutators
Files in mindtwo/laravel-dynamic-model-mutators
Package laravel-dynamic-model-mutators
Short Description Laravel Dynamic Model Mutators
License MIT
Informations about the package laravel-dynamic-model-mutators
Laravel Dynamic Model Mutators
Important Notice:
The following documentation refers to packacke version 1.x
We will add documentation for version 2.x as soon as possible. Sorry.
Introduction
This package is an extension for Laravel's eloquent model. It allows you to define multiple get and set mutators by registering your own callback functions. This is a simple way to inject Laravel's getAttribute() and setAttribute() methods, especially within different traits on a single model.
Installation
You can install the package via composer:
How to use?
Use the "DynamicModelMutator" trait in your eloquent models:
In the boot method of the model you can now register the dynamic getter and setter functions e.g.:
registerSetMutator and registerGetMutator expecting two parameters. First is name of mutator, which is the name of the property which configures your attributes for the registered mutator, too. The second is the name of the callback function which will be called on the same object.
Defining attributes for mutators
To define attributes for your mutators you have to set up a class property with the mutator key as its name. For example we use 'translations' as mutator key, so we can setup our attributes by defining an $translations property on our model.
Optionally you can set up a attribute based configuration which will be passed to you mutator methods. So you can add additional configuration to your attributes.
Set mutator callback function
The callback function for a set mutators must accept three arguments. First is the attribute name, second is the attribute value, third is the attribute specific configuration. Because they will be called automatically within the trait, there is no need to return any value.
Get mutator callback function
The callback function for a get mutators must accept two arguments. First is the attribute name, second is the attribute specific configuration. It should return the attribute value for a given attribute name. Note, that you can not use Laravel's attribute casting feature for a dynamic mutated attribute, but it's quite easy to implement our own castings.
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
- mindtwo GmbH
- All Other Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-dynamic-model-mutators with dependencies
illuminate/database Version ^7.4|^8.0|^9.0|^10.0
illuminate/support Version ^7.4|^8.0|^9.0|^10.0