just use the relation table function name eg echo $section->section->name of the table
Jan 22, 2020
6
Level 1
Laravel eloquent connecting multiple tables
Hello guys, i am attempting to make a select from and combine 3 tables into one:
class SectionAndUser
public function sections()
{ return $this->belongsTo('App\Models\Section');}
public function users()
{return $this->belongsTo('App\Models\User');}
class User
public function sectionAndUser(){return $this->hasMany('App\Models\SectionAndUser');}
class Section
public function section_and_users(){return $this->hasMany('App\Models\SectionAndUser');}
These are the 3 models and their relationships, i would like to select starting from user all the sections he belongs to and then get the remaining info from the table sections.
With the select:$sections = User::find($userId)->sectionAndUser()->get();
i get the result:
"id": 1,
"section_id": 1,
"user_id": 133
},
How do i now connect it with the sections table and pick up section name based on the section_id?
Please or to participate in this conversation.