Answer the question
In order to leave comments, you need to log in
Why don't laravel mutators work with joins?
Good afternoon!
Can you please tell me why getters don't work with joins? Those. here, for example, I have an avatar getter from the users table. In the model, the code is:
public function getAvatarAttribute($value)
{
if( $value == null ) {
return asset('img/users/no-avatar.png');
}
else {
return asset('img/users/' . $value);
}
}
$comments = Comments::leftJoin('users', 'users.id', '=', 'comments.user_id')
->get();
Answer the question
In order to leave comments, you need to log in
Well, after all, a selection from the comments to know does not know anything about the user model.
Build relationships between models and fetch with with() or just lazy loading.
This joint is absolutely not needed here in its raw form.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question