laravel如何通过pivot获取四肢表

laravel how to get the extremities tables through the pivot

如何从中间 table 到达末端的两个 table 之一。

例:在user_roletable中,我想从user_id获取对应的用户enter image description here

谢谢...

一旦你拥有 UserRole 的任何一个实例,你就可以得到你的关系。要获取一个用户的所有角色,您可以执行此操作

$user = App\User::find(1);

foreach ($user->roles as $role) {
    echo $role->operations;
}

同样的方法,获取角色的所有用户,使用这个

$role = Role::find(1);

foreach ($role->users as $user) {
    echo $user->email;
}

您可以在 Laravel 文档中找到有关此的所有信息,在 https://laravel.com/docs/5.5/eloquent-relationships#many-to-many

上查看更多信息