Answer the question
In order to leave comments, you need to log in
Relationships of 3 tables in Laravel?
It is necessary to display articles created by users, on which users are subscribed by the user.
There are 3 tables
Users - id fields
Posts - id fields, user_id
Subscriptions - id fields, user_id (user), user_sub_id (who is subscribed to, one value)
Models
class User extends EloquentUser{
public function postsSub(){
return $this->hasManyThrough('App\Post', 'App\Subscription', 'user_id', 'user_id');
}
}
$posts = User::find(1)->postsSub()->toSql();
select * from `posts` inner join `subscriptions` on `subscriptions`.`id` = `posts`.`user_id` where `subscriptions`.`user_id` = 1
select * from `posts` inner join `subscriptions` on `subscriptions`.`user_sub_id` = `posts`.`user_id` where `subscriptions`.`user_id` = 1
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question