I am trying to create a youtube style subscription model for my application. I've had small amounts of success so far, that being that you can follow other users and it stores who is following who in the database. However I am having trouble retrieving the list of people the current user is following.
The database:
users table (id, first_name, last_name, email, password, token, timestamps)
subscriptions Table (id, user_id, subscriber_id, timestamps)
user model:
public function subscriptions()
{
return $this->belongsToMany('App\User', 'subscriptions', 'user_id',
'subscriber_id')->withTimestamps();
}
public function subscribers()
{
return $this->belongsToMany('App\User', 'subscriptions', 'subscriber_id',
'user_id')->withTimestamps();
}
I am using this code to retrieve all of the current users subscriptions (aka who they are following)
$subscriber = Auth::user()->id;
$user = User::find($subscriber);
$userids = $user->subscriptions->lists('user_id');
return $userids;
Now, this code is currently returning a null value. On further investigation this makes sense as the query that is being run is incorrect
SQL Query:
select `user_id` from `users` inner join `subscriptions` on `users`.`id` = `subscriptions`.`subscriber_id` where `subscriptions`.`user_id` = 14
My current set up is trying to look for the wrong thing.
If user 1 follows users 2, 3 and 4 then my code should return 2, 3 and 4.
Can somebody please help correct my relationship set up or the code that I am running? It should be looking for the user_id in the subscriptions table based on the current users subscriber_ID.