PHPackages                             awobaz/eloquent-mutators - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. awobaz/eloquent-mutators

ActiveLibrary[Database &amp; ORM](/categories/database)

awobaz/eloquent-mutators
========================

Reusable accessors/mutators (getters/setters) for Laravel 5's Eloquent

1.0.23(7mo ago)4522.5k↓50%7[2 issues](https://github.com/topclaudy/eloquent-mutators/issues)[6 PRs](https://github.com/topclaudy/eloquent-mutators/pulls)MITPHPPHP ^7.1|^8.0

Since Dec 13Pushed 7mo ago3 watchersCompare

[ Source](https://github.com/topclaudy/eloquent-mutators)[ Packagist](https://packagist.org/packages/awobaz/eloquent-mutators)[ RSS](/packages/awobaz-eloquent-mutators/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (8)Versions (28)Used By (0)

Eloquent Mutators
=================

[](#eloquent-mutators)

**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.

The problem
-----------

[](#the-problem)

Eloquent has support for [accessors and mutators](https://laravel.com/docs/5.7/eloquent-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.

#### Related discussions:

[](#related-discussions)

- [Reusing accessors &amp; mutators](https://stackoverflow.com/questions/37725691/reusing-an-accessors-mutators/37727418#37727418)
- [Simple and organized accessors &amp; mutators](https://github.com/laravel/ideas/issues/1270)

Installation
------------

[](#installation)

The recommended way to install **Eloquent Mutators** is through [Composer](http://getcomposer.org/)

```
$ 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:

1. Open your `config/app.php` and add the following to the `providers` array:

```
Awobaz\Mutator\MutatorServiceProvider::class,
```

2. In the same `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
```

Usage
-----

[](#usage)

### Using the `Awobaz\Mutator\Database\Eloquent\Model` class

[](#using-the-awobazmutatordatabaseeloquentmodel-class)

Simply 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.

### Using the `Awobaz\Mutator\Mutable` trait

[](#using-the-awobazmutatormutable-trait)

If 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.

### Syntax

[](#syntax)

After configuring your model, you may configure accessors and mutators for its attributes.

#### Defining accessors

[](#defining-accessors)

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'],
    ];
}
```

#### Defining mutators

[](#defining-mutators)

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.php` configuration file.

### Defining accessors/mutators extensions

[](#defining-accessorsmutators-extensions)

In the previous examples, we use [accessors/mutators provided](#built-in-accessorsmutators) 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.

```
