-
-
Notifications
You must be signed in to change notification settings - Fork 142
Mutable
Jarek Tkaczyk edited this page May 28, 2015
·
2 revisions
Mutable provides convenient way of defining getter
and/or setter
mutators:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
use Sofa\Eloquence\Eloquence; // base trait
use Sofa\Eloquence\Mutable; // extension trait
class User extends Model {
use Eloquence, Mutable;
/**
* Attributes getter mutators @ Eloquence\Mutable
*
* @var array
*/
protected $getterMutators = [
'name' => 'strtolower|ucwords'
];
/**
* Attributes setter mutators @ Eloquence\Mutable
*
* @var array
*/
protected $setterMutators = [
'email' => 'strtolower'
];
}
Provided mutators must be valid Mutator callables
$user = new App\User;
$user->email = 'JarekTkaczyk@SOFTonSOFA.com';
$user->email;
// 'jarektkaczyk@softonsofa.com'
$user->name = 'JAREK TKACZYK';
$user->toArray();
// array(
// 'email' => 'jarektkaczyk@softonsofa.com',
// 'name' => 'Jarek Tkaczyk'
// )