Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/larapack/attribute-manipulation

Allows multiple traits to manipulate with the attributes of an Eloquent Model.
https://github.com/larapack/attribute-manipulation

Last synced: about 2 months ago
JSON representation

Allows multiple traits to manipulate with the attributes of an Eloquent Model.

Awesome Lists containing this project

README

        

# attribute-manipulation
Allows multiple traits to manipulate with the attributes of an Eloquent Model.

## Installing

Install using Composer `composer require larapack/attribute-manipulation 1.*`.

## Why?

Some traits might want to manipulate with attributes of an Eloquent Model, this can however be done easy creating the `getAttribute`-method and/or `setAttribute`-method.
However if multiple traits uses those methods then they will complain about eachother since it is only allow for a class to use one trait with the same method.
So if you are up to having a multiple traits using the same methods to manipulate the Eloquent Model attributes then you might use our methods instead for better support.

## Trait setup

Here is how a trait using our methods should look like:

```
password = 'secret' will be saved as the encrypted value of `secret`.
// For the real encryptable trait which this example is based on please view https://github.com/larapack/attribute-encryption
return \Crypt::encrypt($value);
});

// then we will set our manipulator for the attribute getter
static::addGetterManipulator(function($model, $key, $value) {
// here we have both the model object ($model), attribute key ($key) and the current value to set ($value).
// whatever we return is that value that will be sent to the next manipulator
// or if none then it will returned to the output.
// For this example we will return the decrypted value.
// Example: echo $model->password will output the decrypted value of `password`.
// For the real encryptable trait which this example is based on please view https://github.com/larapack/attribute-encryption
return \Crypt::decrypt($value);
});
}
}
```

Example model:
```
password = 'secret';
dump($user); // here you will see that the password is encrypted
echo $user->password; // this will output the decrypted password
```