0
votes

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.

2

2 Answers

3
votes

Laravel 5.3 Eloquent Relationships

If you check the code at the end of the Many-to-Many relationships part, you will see this line of code.

return $this->belongsToMany('App\Role', 'role_user', 'user_id', 'role_id');

Here, we have the 'role_user' as the pivot (relationship) table, 'user_id' which is the foreign key in relationship table and primary key in user model, and 'role_id' is foreign key in relationship table and primary key in roles model.

So when you try to do this:

public function subscriptions()
{
 return $this->belongsToMany('App\User', 'subscriptions', 'user_id',     
 'subscriber_id')->withTimestamps();
}

You are actually taking 'subscriber_id' as a foreign key in your relationship table and primary key for a subscriber. Since the function is trying to get the subscriptions, reaching to the subscriber id's won't do the work. This is why when you changed them they worked.

The result should look like this:

public function subscribers()
{
 return $this->belongsToMany('App\User', 'subscriptions', 'user_id',     
 'subscriber_id')->withTimestamps();
}

public function subscriptions()
{
return $this->belongsToMany('App\User', 'subscriptions', 'subscriber_id', 
'user_id')->withTimestamps();
}
1
votes

So out of pure chance, I swapped the subscribers() and subscriptions() relationships about and it's now returning the correct data.. I don't understand why but it is definitely returning the ID's that I was expecting.

I'll keep the question open for now in case anyone with a clear solution answers.