Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

osherdo's avatar

many-to-many from the same table

Hello everyone.

I need to create a feature of user following. So I could let users follow each other. This require a pivot table right? but how do I do it if the users are all from the same table?

I am using laravel 5.1.

Thanks.

0 likes
7 replies
willvincent's avatar
Level 54

Your table that stores the follower/following relationships should have two fields that each store a user id...

Something like this:

follows
  - followed_id  (references User ID of user being followed)
  - follower_id  (references user ID of the user who is following that user)

Then in your user model you'd have something like this:

  public function followers() {
    return $this->belongsToMany('User', 'follows', 'followed_id', 'follower_id');
  }

  public function following() {
    return $this->belongsToMany('User', 'follows', 'follower_id', 'followed_id');
  }
1 like
osherdo's avatar

@willvincent thanks a lot for answering.

I do have that pivot table you're talking about. here's the migration if it:

{
        Schema::table('follower_followee', function (Blueprint $table) 
        {
            $table->integer('follower_id')->unsigned(); // follower id number,must be positive.
            $table->integer('followee_id')->unsigned(); // followee id number,must be positive.
            $table->foreign('follower_id')->references('id')->on('users')->onDelete('cascade');
            //The 'follower_id' column references to the 'id' column in a 'users' table.
            //When a user is deleted in the parent column ('follower_id'), then also the user in 'id' ('users') is deleted. 
            $table->foreign('followee_id')->references('id')->on('users')->onDelete('cascade');
        });
    }

Now should I remove the Followee.php model then (after updating the User model)?

willvincent's avatar

Yes, pivot tables don't need models. The belongsto definitions are what make them work.

1 like
osherdo's avatar

okay. May I ask about this line:

    return $this->belongsToMany('User', 'follows', 'follower_id', 'followed_id');

what's the 'User' and 'follows' arguments are for?

Edit: so far I assume 'User' is the model name. And 'follower_id', 'followed_id' are the table columns. But what's 'follows' and does it all work together?

osamamoichawla's avatar

Hi @willvincent sir, I can not add values in pivot table. Please guide me! My scenario is like:

model: Category There can be multiple sub categories of the single main category, hence there is a many to many relationship. Like a Mobile is main category the sub-category would be samsung, iphone. and like iphone is also a Apple Brand and as well as a Mobile. Many to many. The above steps are great. But I can't add values in pivot table named "categories_parent"

willvincent's avatar

If you want to associate other data with your pivot, you'll probably want to extend the base pivot class with a custom Pivot Model...

Also, by the way, it's usually better to ask a new question than to revive a many-years-old thread ;)

Please or to participate in this conversation.