L
L
lolrofl012018-11-13 21:53:16
Laravel
lolrofl01, 2018-11-13 21:53:16

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);
        }
    }

If you take users directly from the users model, everything is good. If you make a selection of several tables through join, then the getter no longer works. Is there a way to make it fire anyway when it comes to the avatar field in users?
Here is the join code
$comments = Comments::leftJoin('users', 'users.id', '=', 'comments.user_id')
->get();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Aksentiev, 2018-11-13
@lolrofl01

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 question

Ask a Question

731 491 924 answers to any question