0
我有很多關係roles
和permissions
模型。我有一個操作控制器來附加和分離用戶的權限。如何檢查某些權限是否被分離到某個角色?如何檢查模型實例是否附加到laravel 5.3中的相關模型實例?
控制器:
class RolePermissionController extends Controller
{
// POST /roles/1/permissions/2/sync
// BODY {isAllowed: true}
// $role - instance of role model with id == 1
// $permission - instance of permission model with id == 2
// roles and permissions has many to many relationship
public function synchronize(Request $request, Role $role, Permission $permission)
{
$this->authorize($permission);
$this->validate($request, [
'isAllowed' => 'required|boolean'
]);
// I want to check here if the permission is attached to the role
if ($request->input('isAllowed')) {
$role->perms()->attach($permission);
} else {
$role->perms()->detach($permission);
}
}
}
謝謝! $角色的> perms->包含($許可);爲我工作 – Ildar