I have two table names: User and Table Role . I have established many relationships between them using the Role_User pivot table name in Laravel Eloquent. Now I want to show both tabular data along with a view using Eloquent Relationship. (For example, I want to show, in my opinion, the user from the user table contains a role in the cast table)
Here are my eloquent relationships.
public function roles() { return $this->belongsToMany('App\Role'); } public function users() { return $this->belongsToMany('App\User'); }
My request for explicit affection.
$users=User::with('roles')->get(); return view('admin',compact('users'));
I try in my presentation.
@foreach($users as $user) <tr> <td>{{$user->name}}</td> <td>{{$user->email}}</td> <td>{{$user->roles->name}}</td> <td></td> </tr> @endforeach
source share