I just faced a problem in Laravel 5.1 .
Imagine that I have the following:
Model
class User extends Model{
# Mutator
public function getNomeUpperAttribute(){
return strtoupper($this->attributes['nome']);
}
}
Controller
public function index(){
$user = User::find(1);
$user->nome_upper;
}
Return
NAME IN MAIL
But let's suppose I made a JOIN by Eloquent and do not select the name field of the Users table.
public function index(){
$user = User::join('enderecos', 'enderecos.id_user', '=', 'users.id')
->select('rua', 'bairro', 'numero', 'cep')
->where('id_user', '=', 1)
->first();
}
When this happens the page gives an error:
undefined index 'name';
This happens because it enters the role that I set in Model Users .
To make no mistake, I have to do a check like this:
public function getNomeUpperAttribute(){
if(array_key_exists('nome', $this->attributes))
return strtoupper($this->attributes['nome']);
else
return null;
}
But imagine that I have several Accessors to customize various fields. I would have to do this check at all.
The question is: