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

najmulcse's avatar

Relation among three tables

I'm facing a problem to make a relation among the three tables . I have three models:

  1. User
  2. Group
  3. GroupMember

Tables structure are :

i. users (id,name,email) ii. groups (id, user_id,group_name,title) iii. group_members (id,group_id,user_id)

Requirements are: A user can create multiple groups .In a group there has multiple group members . A user can join multiple groups . Please help me by making a relation among the three tables .

I want to get all groups details where a user is joined that specific groups . Also i want to get all group members in a specific group.

Thank you in advanced .

0 likes
24 replies
bunnypro's avatar

You don't need model for the pivot table, so you only need two models for this, the User model and Group model.

  • The User hasMany Group that he created, you can name the relation whatever you want.

  • The User can be a member of some groups and also group can have some user members. So User belongsToMany Group and Group belongsToMany User.

if your pivot table name is not using eloquent naming convention, you have to pass the pivot table name to the second parameter of belongsToMany method.

1 like
al0mie's avatar

yes, you just need to specify relantionship like this

in User

public function groups()
{
     return $this->belongsToMany(Group::class);
}

in Group

public function users()
{
     return $this->belongsToMany(User::class);
}

After you can get group members from the group like

$users = $group->users

and groups of the user

$groups = $user->groups
1 like
najmulcse's avatar

Thank you for your response .

Actually when a user inter into the system , user can just join a specific group without creating his own group . By these two Model its not possible to relation . In this reason i added GroupMember model .

How can possible to make relation ? please tell me .

bunnypro's avatar

why is it not possible ?

you can just add user to the group by

$user->groups()->attach($group);
najmulcse's avatar

here what is the meaning of $group inside attach() ?

najmulcse's avatar

When i want to find my created group its not working , some errors are showed .

I already joined User and Group models using hasMany() relation to get all groups of a user.

najmulcse's avatar

Suppose you are a user . You already created several groups .

example : group name.

  1. laravel
  2. php
  3. html

you want to get your all groups list as well as you joined another groups like Javascript , CSS you want to show your created groups and also your joined groups. Your created groups are :

  1. laravel
  2. php
  3. html

joined groups:

1.Javascript 2.CSS

Also you want to show how many members of your laravel group and their details .

bunnypro's avatar

in your User model now, at less there should be two relations to Group model

first one is the relation of owning group that has relation of hasMany

second one is the relation of joined group that has relation of belongsToMany

in example

// User class
public function owningGroups() {
    return $this->hasMany(Group::class);
}

public function joinedGroups() {
    return $this->belongsToMany(Groups::class);
}

then you can get groups that user created by

$user->owningGroups;

and groups that user joined

$user->joinedGroups;
bunnypro's avatar

what's not working ? what error you got ?

najmulcse's avatar

please make it three model . when multiple users want to join a group , how can i store these user_id in a single row ?? For this reason we need one extra table .

in group_members table i include the data as follows :

id ->user_id ->group_id

  1. ->2->1,
  2. ->4->1,
  3. ->8->2

But you mentioned earlier that two tables can possible to store these data . how ?

bunnypro's avatar

oh i'm sorry i misstyped above, i mean you only need two models and still with three tables.

i will edit it.

bunnypro's avatar

what else ?

everything is right except my misstyped in first comment.

so you have 3 tables users, groups, group_user, and also 2 Models User model and Group model.

najmulcse's avatar

How can write , i can't understand . what can i do by 2 models using three tables?

najmulcse's avatar

I already read it but little bit confused . please , If you create the relations then i will be helpful .Also how can retrieve these data .

thanks

bunnypro's avatar

i have described it above, either the relation or how to retrieve data

babind's avatar

I think what you are trying to achieve is

    User has many groups,
    and Group has many users,

This is like .. I can eat multiple mangos and one mango can be eaten by many people.

Therefore, #model User group

 #tables
       users
      group_user
     groups 

relation is managed by the unique identifier, keys from users table, or from groups table.

          users table 
               id 
               name
               location

         groups table
              id 
              name
              location
              address


         group_user table( this is pivot table)
             id
             user_id
             group_id

            You can write
             http://developed.be/2013/08/30/laravel-4-pivot-table-example-
             attach-and-detach/


            https://laravel.com/docs/4.2/eloquent#many-to-many

Please or to participate in this conversation.