C
C
chelkaz2016-03-23 00:07:04
Laravel
chelkaz, 2016-03-23 00:07:04

I do not understand how to make a connection in the model with a condition. It's possible?

There is a table with files. It also stores user photos.
In the model, user has made a link to this table:
public function photo_user()
{
return $this->belongsTo('App\Models\File');
}
But the point is that in this table the user's photo is also stored in the line
in which the module_id field is equal to user_profile
Now, for example:
User::find(2)->photo_user;
Gives out Null
How to pull out a photo with such conditions?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrzej Wielski, 2016-03-23
@chelkaz

public function photoUser(){
    return $this->belongsTo('App\Models\File')->where('module_id', $this->user_profile);
}
public function getPhotoUserAttribute(){
    return $this->photoUser()->first()->photo_user;
}

User::find(2)->photoUser;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question