lundi 11 janvier 2016

Laravel eloquent model - get rows using intermediate table

I have 3 tables users, posts and photos.

post table has one - one relation to photos & users like, post.user_id=users.id and post_photo_id=photos._id.

I use

public function posts(){
    return $this->hasMany('Post');
}

and I get all the posts by user using $user->posts().

What I need is to get all the photos by user, something like $user->photos.

SELECT photos.* FROM photos JOIN posts ON posts.photo_id=photos.id JOIN users ON users.id=posts.user_id WHERE user_id=1

Note: photos table has just 2 fields, id & photo.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire