Ejemplo n.º 1
0
 public function updateRoles(Request $request, $userId)
 {
     /** @var User $user */
     $user = User::with('roles')->findOrFail($userId);
     DB::transaction(function () use($request, $user) {
         $roleIds = $request->get('role', []);
         foreach (Role::editable()->get() as $role) {
             $hasRole = $user->isA($role->name);
             $shouldHaveRole = array_key_exists($role->id, $roleIds);
             if ($hasRole && $shouldHaveRole === false) {
                 $user->retract($role);
             } elseif ($hasRole === false && $shouldHaveRole) {
                 $user->assign($role);
             }
         }
     });
     return redirect('/admin/users/' . $userId)->withFlashSuccess('Your changes were saved');
 }