2017 © Pedro Peláez
 

library laravel-mutate

Mutate Laravel attributes

image

weebly/laravel-mutate

Mutate Laravel attributes

  • Saturday, May 5, 2018
  • by eaperezc
  • Repository
  • 11 Watchers
  • 9 Stars
  • 5,754 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 4 Forks
  • 0 Open issues
  • 32 Versions
  • 32 % Grown

The README.md

Eloquent Mutators

License Latest Stable Version StyleCI Tests, (*1)

This package allows you to map your model attributes to database columns when the type in the PHP model does not match the type in the database column., (*2)

This could be using $model->ip_address as a string in your eloquent model but storing it as a BINARY(16) in the database for efficiency. Or having a string always encrypted in the DB but readable in clear form within your models., (*3)

Installing

$ composer require weebly/laravel-mutate

To use this package, you'll need to add the ServiceProvider to the providers array in config/app.php if you are not using automatic package discovery:, (*4)

Weebly\Mutate\LaravelMutatorServiceProvider::class

You'll also need to publish the config to config/mutators.php with:, (*5)

$ php artisan vendor:publish --provider='Weebly\Mutate\LaravelMutatorServiceProvider'

Usage

When creating an Eloquent model, you'll need to extend Weebly\Mutate\Database\Model and add $mutate property it:, (*6)

<?php

namespace App\Models;

use Weebly\Mutate\Database\Model;

class User extends Model
{
    /**
     * {@inheritdoc}
     */
    protected $table = 'users';

    /**
     * {@inheritdoc}
     */
    protected $mutate = [
        'id' => 'uuid_v1_binary'
    ];
}

This will automatically serialize/unserialize the id attribute on the User model when getting/setting the attribute from the database. This allows you to no longer need to set accessors/mutator methods on the model directly., (*7)

Note: Unlike the built in Laravel accessors/mutators, this package will serialize the attribute values when they are passed to an Eloquent query builder., (*8)

Included Mutators

  • uuid_v1_binary Will take a uuid version 1, re-order its bytes so that if uuidA was generated before uuidB, then storedUuidA < storedUuidB, and store it in the database as 16 bytes of data. For more information on the re-ordering of bytes, see: https://www.percona.com/blog/2014/12/19/store-uuid-optimized-way/.
  • ip_binary Will take a string representation of an IPv4 or IPv6 and store it as 16 bytes in the database.
  • encrypt_string Will take a non encrypted string and encrypt it when going to the database.
  • hex_binary Will take any hexadecimal string attribute and store it as binary data.
  • unix_timestamp Will take a Carbon date but store it as an integer unix timestamp.

Creating Custom Mutators

To define a custom mutator, you'll need to create a class that implements Weebly\Mutate\Mutators\MutatorContract, and add it to the enabled array in config/mutators.php., (*9)

Note: All attributes are cached on a model instance automatically, so you should not need to add any caching logic at the mutator level., (*10)

When building and registering a Mutator, it is important to know that they are resolved automatically from the Laravel IOC container, which means you may create service providers for them if they require custom constructor arguments., (*11)

<?php

namespace App\Mutators;

use Weebly\Mutate\Mutators\MutatorContract;

class ExampleEncryptMutator implements MutatorContract
{
    /**
     * {@inheritdoc}
     */
    public function serializeAttribute($value)
    {
        return encrypt($value);
    }

    /**
     * {@inheritdoc}
     */
    public function unserializeAttribute($value)
    {
        return decrypt($value);
    }
}

Testing

Running tests:, (*12)

$ ./vendor/bin/phpunit

License

This package is open-sourced software licensed under the 2-Clause BSD license., (*13)

The Versions

05/05 2018

dev-master

9999999-dev

Mutate Laravel attributes

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Elliot Fehr

database laravel uuid eloquent encrypt

05/05 2018

v1.1.18

1.1.18.0

Mutate Laravel attributes

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Elliot Fehr

database laravel uuid eloquent encrypt

05/05 2018

dev-where-not-in

dev-where-not-in

Mutate Laravel attributes

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Elliot Fehr

database laravel uuid eloquent encrypt

01/12 2017

v1.1.17

1.1.17.0

Mutate Laravel attributes

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Elliot Fehr

database laravel uuid eloquent encrypt

01/12 2017

dev-sync-bugfix

dev-sync-bugfix

Mutate Laravel attributes

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Elliot Fehr

database laravel uuid eloquent encrypt

24/11 2017

v1.1.16

1.1.16.0

Mutate Laravel attributes

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Elliot Fehr

database laravel uuid eloquent encrypt

13/11 2017

v1.1.15

1.1.15.0

Mutate Laravel attributes

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Elliot Fehr

database laravel uuid eloquent encrypt

13/11 2017

dev-new-record-assoc-bugfix

dev-new-record-assoc-bugfix

Mutate Laravel attributes

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Elliot Fehr

database laravel uuid eloquent encrypt

09/11 2017

v1.1.14

1.1.14.0

Mutate Laravel attributes

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Elliot Fehr

database laravel uuid eloquent encrypt

09/11 2017

dev-better-fix-for-double-serialize

dev-better-fix-for-double-serialize

Mutate Laravel attributes

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Elliot Fehr

database laravel uuid eloquent encrypt

08/11 2017

v1.1.13

1.1.13.0

Mutate Laravel attributes

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Elliot Fehr

database laravel uuid eloquent encrypt

07/11 2017

1.1.12

1.1.12.0

Mutate Laravel attributes

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Elliot Fehr

database laravel uuid eloquent encrypt

05/11 2017

v1.1.11

1.1.11.0

Mutate Laravel attributes

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Elliot Fehr

database laravel uuid eloquent encrypt

05/11 2017

dev-eloquent-inconsistency-bugfix

dev-eloquent-inconsistency-bugfix

Mutate Laravel attributes

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Elliot Fehr

database laravel uuid eloquent encrypt

10/10 2017

v1.1.10

1.1.10.0

Mutate Laravel attributes

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Elliot Fehr

database laravel uuid eloquent encrypt

05/10 2017

v1.1.9

1.1.9.0

Mutate Laravel attributes

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Elliot Fehr

database laravel uuid eloquent encrypt

05/10 2017

dev-cache-me-ousside-bugfix

dev-cache-me-ousside-bugfix

Mutate Laravel attributes

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Elliot Fehr

database laravel uuid eloquent encrypt

05/10 2017

v1.1.8

1.1.8.0

Mutate Laravel attributes

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Elliot Fehr

database laravel uuid eloquent encrypt

05/10 2017

dev-belongs-to-many-eager-bugfix

dev-belongs-to-many-eager-bugfix

Mutate Laravel attributes

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Elliot Fehr

database laravel uuid eloquent encrypt

29/09 2017

v1.1.7

1.1.7.0

Mutate Laravel attributes

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Elliot Fehr

database laravel uuid eloquent encrypt

29/09 2017

v1.1.6

1.1.6.0

Mutate Laravel attributes

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Elliot Fehr

database laravel uuid eloquent encrypt

29/09 2017

v1.1.5

1.1.5.0

Mutate Laravel attributes

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Elliot Fehr

database laravel uuid eloquent encrypt

29/09 2017

dev-relationship-actual-fix

dev-relationship-actual-fix

Mutate Laravel attributes

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Elliot Fehr

database laravel uuid eloquent encrypt

28/09 2017

dev-pivot-mutators

dev-pivot-mutators

Mutate Laravel attributes

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Elliot Fehr

database laravel uuid eloquent encrypt

28/09 2017

v1.1.4

1.1.4.0

Mutate Laravel attributes

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Elliot Fehr

database laravel uuid eloquent encrypt

28/09 2017

v1.1.3

1.1.3.0

Mutate Laravel attributes

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Elliot Fehr

database laravel uuid eloquent encrypt

22/09 2017

v1.1.2

1.1.2.0

Mutate Laravel attributes

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Elliot Fehr

database laravel uuid eloquent encrypt

21/09 2017

dev-bugfix-closure-query

dev-bugfix-closure-query

Mutate Laravel attributes

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Elliot Fehr

database laravel uuid eloquent encrypt

21/09 2017

v1.1.1

1.1.1.0

Mutate Laravel attributes

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Elliot Fehr

database laravel uuid eloquent encrypt

11/09 2017

dev-develop

dev-develop

Mutate Laravel attributes

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Elliot Fehr

database laravel uuid eloquent encrypt

11/09 2017

v1.1.0

1.1.0.0

Mutate Laravel attributes

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Elliot Fehr

database laravel uuid eloquent encrypt

07/09 2017

v1.0.0

1.0.0.0

Mutate Laravel attributes

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Elliot Fehr

database laravel uuid eloquent encrypt