In general, the controller sends 2 arrays to the view

class Admin_controller extends Controller { public function monitoring_user() { $data=[ 'users'=> User::join('user_info', 'users.id', '=', 'user_id')->get(), 'role' => Role::join('users_roles','roles.id','=','role_id')->get() ]; return view('monitoring_user' ,$data); 

And in it is necessary to process in one iteration 2 arrays

  @if (count($users)) @foreach($users as $user) <tr> <td><center>{!! $user->login !!}</center></td> <td><center>{!! $user->name !!}</center></td> <td><center>{!! $user->surname !!}</center></td> <td><center>{!! $user->patronymicname !!}</center></td> /// <td><center>{!! $role->name !!}</center> <td> @endforeach 

I’m sorting out users well, but I also need to bring the roles right there I apologize for the crooked explanation and for my ignorance

  • Well, make @foreach - u_mulder nested
  • Nested cannot be because it’s a single row of the table, if I make a nested it will output all the roles, but I need one @u_mulder - Zoidberg
  • one
    So you don’t need everything, you first need to check every element of the nested forych - is its user_id equal to the current one? If yes, print the data of the current element, if not, look into the next one. - u_mulder
  • Oh thank you so much that I protup @u_mulder - Zoidberg
  • 2
    Of course, I don’t know how your models are arranged there, but maybe these two entities can somehow be set to work so that with the user his roles immediately fall out? Then you will manage one forych. - u_mulder

1 answer 1

You need to build a query using the JOIN operator, and then you do not need to connect these arrays and invent something with a data reassembly at the same time, since this cannot be done in php!

 SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons LEFT JOIN Orders ON Persons.P_Id=Orders.P_Id ORDER BY Persons.LastName 

Here is a link to the Russian documentation