Пример #1
0
 /**
  * 分配权限
  */
 public function assignPermission(Request $request)
 {
     $role = Defender::findRoleById($request->get('id'));
     $permissionNameParams = $request->except(['_token', 'id']);
     $permissionNames = array_keys($permissionNameParams);
     $permissions = [];
     foreach ($permissionNames as $permissionName) {
         $permissions[] = str_replace('_', '.', $permissionName);
     }
     // 删除 roles 所有权限
     $role->revokePermissions();
     $permissions = Permission::whereIn('name', $permissions)->get();
     $permissions->each(function ($per) use($role) {
         $role->attachPermission($per);
     });
     return redirect()->route('admin.roles.index');
 }
Пример #2
0
 /**
  * 分配角色
  */
 public function assignRole(Request $request)
 {
     $admin = Admin::findOrFail($request->get('id'));
     // detach roles
     $admin->roles()->detach();
     $role = Defender::findRoleById($request->get('role_id'));
     $admin->attachRole($role);
     return redirect()->route('admin.admins.index');
 }