Reusable accessors/mutators (getters/setters) for Laravel 5's Eloquent
awobaz/eloquent-mutators is a Laravel package for reusable accessors/mutators (getters/setters) for laravel 5's eloquent.
It currently has 45 GitHub stars and 23.201 downloads on Packagist (latest version 1.0.23).
Install it with composer require awobaz/eloquent-mutators.
Discover more Laravel packages by awobaz
or browse all Laravel packages to compare alternatives.
Last updated
Eloquent Mutators allows us to define accessors and mutators outside of an Eloquent model. This gives us the ability to organize and reuse them on any model or any attribute of the same model.
Eloquent has support for accessors and mutators. However, it requires us to define them directly in the model. What if we want to reuse an accessor/mutator logic in another model? Or, what if we want to reuse an accessor/mutator logic for another attribute of the same model? We can't! Eloquent Mutators aims at solving this limitation.
The recommended way to install Eloquent Mutators is through Composer
$ composer require awobaz/eloquent-mutators
The package will automatically register itself if you're using Laravel 5.5+. For Laravel 5.4, you'll have to register the package manually:
config/app.php and add the following to the providers array:Awobaz\Mutator\MutatorServiceProvider::class,
config/app.php add the following to the aliases array:'Mutator' => Awobaz\Mutator\Facades\Mutator::class,
Note: Eloquent Mutators requires Laravel 5.4+.
After installation, publish the assets using the mutators:install Artisan command. The primary configuration file will be located at config/mutators.php. The installation also publishes and registers the app/Providers/MutatorServiceProvider.php. Within this service provider, you may register custom accessors/mutators extensions.
php artisan mutators:install
Awobaz\Mutator\Database\Eloquent\Model classSimply make your model class derive from the Awobaz\Mutator\Database\Eloquent\Model base class. The Awobaz\Mutator\Database\Eloquent\Model extends the Eloquent base class without changing its core functionality.
Awobaz\Mutator\Mutable traitIf for some reasons you can't derive your models from Awobaz\Mutator\Database\Eloquent\Model, you may take advantage of the Awobaz\Mutator\Mutable trait. Simply use the trait in your models.
After configuring your model, you may configure accessors and mutators for its attributes.
For the following Post model, we configure accessors to trim whitespace from the beginning and end of the title and content attributes:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use \Awobaz\Mutator\Mutable;
protected $accessors = [
'title' => 'trim_whitespace',
'content' => 'trim_whitespace',
];
}
As you can see, we use an array property named accessors on the model to configure its accessors. Each key of the array represents the name of an attribute, and the value points to one or multiple accessors. To apply multiple accessors, pass an array as value (the accessors will be applied in the order they are specified):
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use \Awobaz\Mutator\Mutable;
protected $accessors = [
'title' => ['trim_whitespace', 'capitalize'],
'content' => ['trim_whitespace', 'remove_extra_whitespace'],
];
}
To define mutators, use an array property named mutators instead.
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use \Awobaz\Mutator\Mutable;
protected $mutators = [
'title' => 'remove_extra_whitespace',
];
}
Note: The name of the properties used for accessors and mutators can be respectively configured in the
config/mutators.phpconfiguration file.
In the previous examples, we use accessors/mutators provided by the package. You may also register accessors/mutators extensions using the extend method of the Mutator facade. The extend method accepts the name of the accessor/mutator and a closure.
<?php
namespace App\Providers;
use Awobaz\Mutator\Facades\Mutator;
use Illuminate\Support\ServiceProvider;
class MutatorServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//Register your custom accessors/mutators extensions here.
Mutator::extend('extension_name', function($model, $value, $key){
//DO STUFF HERE AND RETURN THE VALUE
});
}
}
As you can see, the model ($model), the attribute's value ($value) and the attribute's name ($key) are passed to the closure, allowing you to access other attributes of the model to compute and return the desired value.
You can also define additional parameters for an extension. This give us the flexibility to implement dynamic accessors/mutators.
<?php
namespace App\Providers;
use Awobaz\Mutator\Facades\Mutator;
use Illuminate\Support\ServiceProvider;
class MutatorServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//The following extension is an implementation for str_replace
Mutator::extend('str_replace', function ($model, $value, $key, $search, $replace) {
return str_replace($search, $replace, $value);
});
}
}
In the above example, the model ($model), the attribute's value ($value), the attribute's name ($key) and two additional parameters are passed to the closure.
To apply this extension, we can use the following syntaxes:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use \Awobaz\Mutator\Mutable;
protected $accessors = [
'content' => ['str_replace' => ['one', 'two']]
//OR
//'content' => 'str_replace:one,two'
];
}
This will replace every occurence of one with two for the content attribute.
lower_caseupper_casecapitalizecapitalize_wordstrim_whitespacecamel_casesnake_casekebab_casestudly_casetitle_casepluralsingularslugremove_extra_whitespacepreg_replace:pattern,replacement[,limit]lower_caseConvert the attribute to lower case.
upper_caseConvert the attribute to upper case.
capitalizeConvert the first character of attribute to upper case.
capitalize_wordsConvert the first character of each word of the attribute to upper case.
trim_whitespaceStrip whitespace from the beginning and end of the attribute.
camel_caseConvert the attribute to camel case.
snake_caseConvert the attribute to snake case.
studly_caseConvert the attribute to studly case.
kebab_caseConvert the attribute to kebab case.
title_caseConvert the attribute to title case.
pluralConvert the attribute to its plural form (only supports the English language).
singularConvert the attribute to its singular form (only supports the English language).
slugConvert the attribute to its URL friendly "slug" form.
remove_extra_whitespaceRemove extra whitespaces within the attribute.
preg_replace:pattern,replacement[,limit]Perform a regular expression search and replace on the attribute.
We use SemVer for versioning. For the versions available, see the tags on this repository.
In order to run the test suite, install the development dependencies:
$ composer install --dev
Then, run the following command:
$ vendor/bin/phpunit
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests.
Eloquent Mutators is licensed under the MIT License.