Maybe of course I read the documentation incorrectly, but many-to-many links work with find() , but if I need to post the entire value for which id_user is equal to the authorized user. That is where my problem came out. Is it possible for me to shorten the code?

 $directions = Direction::where('id_user', Auth::user()->id)->with('executes'); $execute = []; foreach ($directions->get() as $value){ foreach (Direction::find($value->id)->executes()->get() as $item){ $execute[] = $item->name; } } dd($execute); 

Tried such value $roles = Direction::find(1)->executes(); Only minus I need all the values ​​that fall under the condition id_user = Auth::user()->id in the Direction model. I did not understand the hasManyThrough account, whether or not the table is right for me, I have the following Direction

 id name id_user 

Execute

 id name id_user 

Execute_direction

 direction_id execute_id 
  • And why id_user do not store in the table Execute_direction ? - Vanya Avchyan
  • Why do you need it? same intermediate table - Ruslan
  • I just do not see the point of adding him there - Ruslan

1 answer 1

 $executors = Executor::whereHas('directions', function($query) { $query->where('id_user', auth()->user()->id); }) 
  • one
    auth()->user()->id - Vanya Avchyan