Laravel Belongstomany

Asked

Viewed 263 times

3

I have a user table that makes relationship with profile belongsTo, and the profile relates to area items belongsToMany , and area items makes relationship with area belongsTo, he is bringing my item from the area, but I would like to bring tbm to my area in the data, someone would know me how to do this ?

I’m making the call as follows

Auth::user()->profiles->areasItens->toArray();
  • only keep calling the relationship, ->area->areaItens->toArray since area contains items, so area one to n

2 answers

2

Use your User model for example:

$user = User::with(['profiles.areasItens'])
         ->findOrFail(Auth::user()->id);

print_r($user->toArray());

1

I believe with solves your problem:

Auth::user()->with("profiles", 
     "profiles.areaItens", 
     "profiles.areaItens.area")
     ->profiles->areaItens->toArray();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.