I have 4 tables event, group, category, judoka. Event has many groups, group has many categories and category has many judokas. So how can i realize this? I tried to create pivot tables event_group, group_category, category_judoka, but when I put judokas to category they are in all events not just in one. I have many to many relations between all tables. Any suggestions?

3 Answers

0
Evaldas Kuslevic On Best Solutions

Thank you all, i found a solution. Just created pivot table with 3 values category_id, event_id and judoka_id. Attached values like this: $categoryf->events()->attach($categoryf->id, ['event_id' => $event->id, 'judoka_id' => $judokas->id]);

0
Farshad On

you have to define the reverse of relation for us to know how to best answer that but here is what You can Do it you just need a 1 to n relation ship between them all : here in your Event model :

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

and in your Group model :

 public function category(){
    return $this->hasMany(Category::class);
    }

and in your Category Model :

public function judokas(){
        return $this->hasMany(Judokas::class);
}

and consider to put a modelname_id in your related table so for example in your event model you need a column like :

group_id

and in your Group model you need a column like :

category_id

and in Your Category model you need a column like :

jukodas_id

and now you can access all the relation by nestest relationships so for example in your controller you can use thew like below :

 $data = Event::with('Group', 'category.judokas');

here you can get the nested relationship which is the category relation inside the judokas . i hope it helps

0
alirezaBahmei On

your question is not clear but based on your description you should have this relations :

  1. events has many categories
  2. events has many judokas
  3. groups has many judokas

when you add judoka to cateory you should sync this relations :

category->events()->judokas()
category->groups()->judokas()

and access them like this:

event->judokas()
group->judokas()