Reputation: 1455
I have 3 tables like:
| user_table | | user_image_table| | images_table |
----------------------------------------------------------
id_user | | id_user | | id_image |
| | id_image | | |
Problem is that if user is logged in I can't get images that belongs to that user. I have tried:
hasManyThrough(Image::class, UserImage::class, 'id_user', 'id_image')
in User
class. But this has not helped. Is there anyway to do it?
Upvotes: 0
Views: 27
Reputation: 1309
You need to use belongsToMany
relationship in both Image
and User
models.
Image model:
public function users(){
return $this->belongsToMany(User::class, 'user_image_table', 'id_image', 'id_user');
}
User model:
public function images(){
return $this->belongsToMany(Image::class, 'user_image_table', 'id_user', 'id_image');
}
and then access logged in user's images via Auth::user()->images
or request()->user()->images
Upvotes: 3