Waleed Kasem
Waleed Kasem

Reputation: 145

Laravel 5 Relations

Database Structure:

-Users Table -user_id -name -...

-Follow Table -user_id -follow_id

so when user follow another it will be inserted in follow table

and when get user followers

 $user  = User::where('user_id',$id)->first();


    $user['followers'] = $user->Followers;
    $user['following'] = $user->Following;

return $user;

By This Relation in User Model Side

public function Followers()
{
    return $this->hasMany('App\Follow','follow_id','user_id');

}

And By This Relation in Follow Model Side

public function getUserData()
    {
    return $this->belongsTo('App\User','user_id','user_id');
   }

It Works Fine for me and it gaves me every id But the problem is

that i want to get information about every user returned from this relation

so i should call User Model for each user returned to get his information or what ??

Upvotes: 0

Views: 169

Answers (1)

Rwd
Rwd

Reputation: 35200

The way you have you many-to-many relationship set up is almost right.

Firstly, change the Followers() method to be followers() as Laravel follows the PSR-2 standard.

Secondly, this isn't essential but change the user_id column in the users table to be just id. This is a laravel convension that does not need to be followed, however, at this point I don't see any reason not to follow it. I am assuming that you have something like:

protected $primaryKey = 'user_id';

in your User model. If you change your user_id column to id you won't have to have the above declaration anymore. (If you don't have that line in and you want to continue using user_id as the primary key you WILL have to add that line to your User model.

Thirdly, change the relationship type in followers() to be:

public function followers()
{
    return $this->belongsToMany('App\User', 'follower', 'user_id', 'follow_id'); 
    //follower is the table name, user_id is column that relates to the current model and follow_id is the column that is for the relationships
}

With all of the above done you can now get a user with all of their followers by doing:

$user = User::with('followers')->find($id);

This will allow you to get the followers by simply doing:

$user->followers

At this point you can not get rid of you Follow model as you generally wont need a model for a pivot table.

To get the following relationship just add:

public function following()
{
    return $this->belongsToMany('App\User', 'follower', 'follow_id', 'user'); 
}

to your User model.

Again to access this you can either:

$user = User::with('following')->find($id);

or if you have already have the user model and the relationship isn't loaded you can:

$user->load('following'); //in this case following is the name of the method where you have declared the relationship.

For more information please refer to the documentation http://laravel.com/docs/5.1/eloquent-relationships#many-to-many and http://laravel.com/docs/5.1/eloquent

Hope this helps!

Upvotes: 2

Related Questions