The following documentation refers to packacke version 1.x
We will add documentation for version 2.x as soon as possible. Sorry.
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.
You can install the package via composer:
composer require mindtwo/laravel-dynamic-model-mutators
Use the "DynamicModelMutator" trait in your eloquent models:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use mindtwo\LaravelDynamicModelMutators\DynamicModelMutator;
class User extends Model
{
use DynamicModelMutator;
}
In the boot method of the model you can now register the dynamic getter and setter functions e.g.:
/**
* The "booting" method of the model.
*
* @return void
*/
protected static function boot()
{
static::registerSetMutator('translations', 'setAttributeTranslation');
static::registerGetMutator('translations', 'getAttributeTranslation');
}
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.
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.
namespace Examples;
use Illuminate\Database\Eloquent\Model;
use mindtwo\LaravelDynamicModelMutators\DynamicModelMutator;
use mindtwo\LaravelDynamicModelMutators\Interfaces\DynamicModelMutatorInterface;
class exampleModel extends Model implements DynamicModelMutatorInterface
{
use use DynamicModelMutator;
$translations = [
'attribute_1',
'attribute_2',
'attribute_3'
];
}
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.
namespace Examples;
use Illuminate\Database\Eloquent\Model;
use mindtwo\LaravelDynamicModelMutators\DynamicModelMutator;
use mindtwo\LaravelDynamicModelMutators\Interfaces\DynamicModelMutatorInterface;
class exampleModel extends Model implements DynamicModelMutatorInterface
{
use use DynamicModelMutator;
$translations = [
'attribute_1' = 'string',
'attribute_2' = 'text',
'attribute_3' = [
'type' => string,
'locales' => ['en', 'de']
]
];
}
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.
/**
* Set attribute translation.
*
* @return void
*/
public function setAttributeTranslation($name, $value, $config=null)
{
// Set value $value for attribute $name
}
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.
/**
* Get attribute translation.
*
* @return void
*/
public function setAttributeTranslation($name, $config=null)
{
// Get value for attribute $name
}
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.