class YourModel extends Model {
protected $fields = ['name'];
protected $method = [];
public function __construct()
{
foreach($this->fields as $f)
{
$upperFunction = function($value) {
return strtoupper($value);
};
$mutatorMethod = 'get'.studly_case($f).'Attribute';
$this->method[$mutatorMethod] = \Closure::bind($upperFunction, $this, get_class());
}
}
// public function getNameAttribute($value)
// {
// return strtoupper($value);
// }
public function __call($method, $args) {
if(is_callable($this->method[$method]))
{
return call_user_func_array($this->method[$method], $args);
}
}
/**
* Determine if a get mutator exists for an attribute.
*
* @param string $key
* @return bool
*/
public function hasGetMutator($key)
{
$method = 'get'.studly_case($key).'Attribute';
return method_exists($this, $method) || is_callable(isset($this->method[$method]) ? $this->method[$method] : null);
}
}