Relationships of 3 tables in Laravel?

To list articles created by users, which users are subscribed.
There are 3 tables
Users - id field
Posts - fields id, user_id
Subscriptions - fields id, user_id(user), user_sub_id(who signed, one value)

Model
class User extends EloquentUser{
 public function postsSub(){
 return $this->hasManyThrough('App\Post', 'App\Subscription', 'user_id', 'user_id');
}
}


In the controller, do the following
$posts = User::find(1)->postsSub()->toSql();

Receive a request
select * from `posts` inner join `subscriptions` on `subscriptions`.`id` = `posts`.`user_id` where `subscriptions`.`user_id` = 1


I need to query instead of `subscriptions`.`id` = `posts`.`user_id` was `subscriptions`.`user_sub_id` = `posts`.`user_id`

Actually how do you make such a request

select * from `posts` inner join `subscriptions` on `subscriptions`.`user_sub_id` = `posts`.`user_id` where `subscriptions`.`user_id` = 1


Well, or as it is else can be done?
June 8th 19 at 17:19
1 answer
June 8th 19 at 17:21
Use belongsToMany

Find more questions by tags Laravel