當前位置: 首頁>>代碼示例>>PHP>>正文


PHP Role::findOrFail方法代碼示例

本文整理匯總了PHP中app\models\Role::findOrFail方法的典型用法代碼示例。如果您正苦於以下問題:PHP Role::findOrFail方法的具體用法?PHP Role::findOrFail怎麽用?PHP Role::findOrFail使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在app\models\Role的用法示例。


在下文中一共展示了Role::findOrFail方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: destroy

 /**
  * Removes the specified user from the specified role.
  *
  * @param int|string $roleId
  * @param int|string $userId
  *
  * @return \Illuminate\Http\RedirectResponse
  */
 public function destroy($roleId, $userId)
 {
     $this->authorize('admin.roles.users.destroy');
     $role = $this->role->findOrFail($roleId);
     $user = $role->users()->findOrFail($userId);
     // Retrieve the administrators name.
     $adminName = Role::getAdministratorName();
     // Retrieve all administrators.
     $administrators = $this->user->whereHas('roles', function ($query) use($adminName) {
         $query->whereName($adminName);
     })->get();
     $admin = Role::whereName($adminName)->first();
     // We need to verify that if the user is trying to remove all roles on themselves,
     // and they are the only administrator, that we throw an exception notifying them
     // that they can't do that. Though we want to allow the user to remove the
     // administrator role if more than one administrator exists.
     if ($user->hasRole($admin) && $user->id === auth()->user()->id && count($administrators) === 1) {
         flash()->setTimer(null)->error('Error!', "Unable to remove the administrator role from this user. You're the only administrator.");
         return redirect()->route('admin.roles.show', [$roleId]);
     }
     if ($role->users()->detach($user)) {
         flash()->success('Success!', 'Successfully removed user.');
         return redirect()->route('admin.roles.show', [$roleId]);
     }
     flash()->error('Error!', 'There was an issue removing this user. Please try again.');
     return redirect()->route('admin.roles.show', [$roleId]);
 }
開發者ID:stevebauman,項目名稱:ithub,代碼行數:35,代碼來源:RoleUserController.php

示例2: destroy

 /**
  * Removes the specified permission from the specified role.
  *
  * @param int|string $roleId
  * @param int|string $permissionId
  *
  * @return int
  */
 public function destroy($roleId, $permissionId)
 {
     $this->authorize('admin.roles.permissions.destroy');
     $role = $this->role->findOrFail($roleId);
     $permission = $role->permissions()->findOrFail($permissionId);
     return $role->permissions()->detach($permission);
 }
開發者ID:stevebauman,項目名稱:administration,代碼行數:15,代碼來源:RolePermissionProcessor.php

示例3: destroy

 /**
  * Removes the specified permission from the specified role.
  *
  * @param int|string $roleId
  * @param int|string $permissionId
  *
  * @return \Illuminate\Http\RedirectResponse
  */
 public function destroy($roleId, $permissionId)
 {
     $this->authorize('admin.roles.permissions.destroy');
     $role = $this->role->findOrFail($roleId);
     $permission = $role->permissions()->findOrFail($permissionId);
     if ($role->permissions()->detach($permission)) {
         flash()->success('Success!', 'Successfully removed permission.');
         return redirect()->route('admin.roles.show', [$roleId]);
     }
     flash()->error('Error!', 'There was an issue removing this permission. Please try again.');
     return redirect()->route('admin.roles.show', [$roleId]);
 }
開發者ID:stevebauman,項目名稱:ithub,代碼行數:20,代碼來源:RolePermissionController.php

示例4: store

 /**
  * Store role
  *
  * @param array $roleData            
  * @throws NotFoundException, ValidationException
  * @return \App\Models\RoleModel
  */
 public function store($roleData)
 {
     try {
         if (array_get($roleData, 'id')) {
             $role = RoleModel::findOrFail((int) array_get($roleData, 'id'))->fill($roleData);
         } else {
             $role = new RoleModel();
             $role->fill($roleData);
         }
     } catch (Exception $e) {
         throw new NotFoundException(trans('app.notFound'));
     }
     if (!$role->validate()) {
         throw new ValidationException(trans('app.correctErrors'), $role->errors()->toArray());
     }
     try {
         $role->save();
         // associate permissions
         if (array_get($roleData, 'permission_id')) {
             $role->perms()->sync(array_get($roleData, 'permission_id'));
         }
     } catch (Exception $e) {
         throw $e;
     }
     return $role;
 }
開發者ID:jasekz,項目名稱:filament-for-laravel-5,代碼行數:33,代碼來源:Role.php

示例5: update

 /**
  * Update the specified resource in storage.
  *
  * @param Request $request
  * @param int $id
  *
  * @return mixed
  */
 public function update(Request $request, $id)
 {
     $this->validate($request, ['role' => 'required|unique:roles,role,' . $id]);
     $role = Role::findOrFail($id);
     $role->update($request->all());
     \Flash::success('Role updated!');
     return redirect('admin/data-management/roles');
 }
開發者ID:troccoli,項目名稱:lva,代碼行數:16,代碼來源:RolesController.php

示例6: revokeRole

 public function revokeRole($id, $role_id)
 {
     $this->authorize('user_revoke_role');
     $user = User::find($id);
     $role = Role::findOrFail($role_id);
     $user->revokeRole($role);
     return redirect()->back();
 }
開發者ID:phelipperibeiro,項目名稱:book_store_laravel,代碼行數:8,代碼來源:UsersController.php

示例7: destroy

 /**
  * Deletes the specified role.
  *
  * @param int|string $id
  *
  * @throws CannotDeleteAdministratorRole
  *
  * @return bool
  */
 public function destroy($id)
 {
     $this->authorize('admin.roles.destroy');
     $role = $this->role->findOrFail($id);
     if ($role->isAdministrator()) {
         throw new CannotDeleteAdministratorRole("You can't delete the administrator role.");
     }
     return $role->delete();
 }
開發者ID:stevebauman,項目名稱:administration,代碼行數:18,代碼來源:RoleProcessor.php

示例8: getEdit

 public function getEdit($id)
 {
     //fetch the role here just to force a 404 if it doesnt exit, we fetch it via ajax for the display anyway.
     $role = Role::findOrFail($id);
     if ($role->name == 'administrator') {
         abort(404);
     }
     return view('admin.roles.edit')->with('page_title', trans('admin.roles_title'))->with('id', $id);
 }
開發者ID:fluentkit,項目名稱:fluentkit,代碼行數:9,代碼來源:RolesController.php

示例9: updateRole

 public function updateRole($request)
 {
     $id = $request->route('role');
     $model = Role::findOrFail($id);
     $model->fill(['name' => $request->name, 'label' => $request->label]);
     $permissions = array_flatten($request->permissions);
     $model->permissions()->sync($permissions);
     return $model->save();
 }
開發者ID:jaumesala,項目名稱:opendata-maps,代碼行數:9,代碼來源:RoleRepository.php

示例10: destroy

 /**
  * Removes the specified user from the specified role.
  *
  * @param int|string $roleId
  * @param int|string $userId
  *
  * @throws CannotRemoveRolesException
  *
  * @return int
  */
 public function destroy($roleId, $userId)
 {
     $this->authorize('admin.roles.users.destroy');
     $role = $this->role->findOrFail($roleId);
     $user = $role->users()->findOrFail($userId);
     // Retrieve the administrators name.
     $adminName = Role::getAdministratorName();
     // Retrieve all administrators.
     $administrators = $this->user->whereHas('roles', function (Builder $builder) use($adminName) {
         $builder->whereName($adminName);
     })->get();
     $admin = Role::whereName($adminName)->first();
     // We need to verify that if the user is trying to remove all roles on themselves,
     // and they are the only administrator, that we throw an exception notifying them
     // that they can't do that. Though we want to allow the user to remove the
     // administrator role if more than one administrator exists.
     if ($user->hasRole($admin) && $user->getKey() === auth()->user()->getKey() && count($administrators) === 1) {
         throw new CannotRemoveRolesException("Unable to remove the administrator role from this user. You're the only administrator.");
     }
     return $role->users()->detach($user);
 }
開發者ID:stevebauman,項目名稱:administration,代碼行數:31,代碼來源:RoleUserProcessor.php

示例11: update

 /**
  * Update the specified resource in storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function update(Request $request, $id)
 {
     $data = $request->all();
     $validator = Validator::make($data, ['name' => 'max:255', 'display_name' => 'max:255']);
     if ($validator->fails()) {
         $this->throwValidationException($request, $validator);
     }
     $role = Role::findOrFail($id);
     $role->update($data);
     $msg = array('msg' => '已成功更新');
     return json_encode($msg);
 }
開發者ID:nosun,項目名稱:laravel_base,代碼行數:18,代碼來源:RoleController.php

示例12: rules

 /**
  * Get the validation rules that apply to the request.
  *
  * @return array
  */
 public function rules()
 {
     switch ($this->method()) {
         case 'POST':
             return ['name' => 'required|min:3|max:255|unique:roles,name'];
         case 'PUT':
         case 'PATCH':
             $id = $this->route()->roles;
             $role = Role::findOrFail($id);
             return ['name' => 'required|min:3|max:255|unique:roles,name,' . $role->id];
         default:
             break;
     }
 }
開發者ID:gertjanroke,項目名稱:users,代碼行數:19,代碼來源:RolesRequest.php

示例13: destroy

 /**
  * Deletes the specified role.
  *
  * @param int|string $id
  *
  * @return \Illuminate\Http\RedirectResponse
  */
 public function destroy($id)
 {
     $this->authorize('admin.roles.destroy');
     $role = $this->role->findOrFail($id);
     if ($role->isAdministrator()) {
         flash()->setTimer(null)->error('Error!', "You can't delete the administrator role.");
         return redirect()->route('admin.roles.show', [$id]);
     }
     if ($role->delete()) {
         flash()->success('Success!', 'Successfully deleted role.');
         return redirect()->route('admin.roles.index');
     }
     flash()->error('Error!', 'There was an issue deleting this role. Please try again.');
     return redirect()->route('admin.roles.show', [$id]);
 }
開發者ID:stevebauman,項目名稱:ithub,代碼行數:22,代碼來源:RoleController.php

示例14: destroy

 /**
  * Efface la ressource de la bd.
  *
  * @param  int  $id l'id du rôle à effacer
  * @return Response
  */
 public function destroy($id)
 {
     try {
         $role = Role::findOrFail($id);
         $role->delete();
     } catch (ModelNotFoundException $e) {
         App::abort(404);
     }
     return Redirect::action('RolesController@index');
 }
開發者ID:BenoitDesrosiers,項目名稱:GES2015,代碼行數:16,代碼來源:RolesController.php

示例15: init

 public function init()
 {
     return false;
     //分配權限
     $admin = Role::findOrFail(2);
     $user = User::where('name', '=', 'cd')->first();
     // role attach alias
     $user->attachRole($admin);
     // parameter can be an Role object, array, or id
     // or eloquent's original technique
     $user->roles()->attach($admin->id);
     // id only
     //添加權限
     $owner = Role::findOrFail(1);
     $admin = Role::findOrFail(2);
     $createPost = new Permission();
     $createPost->name = 'create-post';
     $createPost->display_name = 'Create Posts';
     // optional
     // Allow a user to...
     $createPost->description = 'create new blog posts';
     // optional
     $createPost->save();
     $editUser = new Permission();
     $editUser->name = 'edit-user';
     $editUser->display_name = 'Edit Users';
     // optional
     // Allow a user to...
     $editUser->description = 'edit existing users';
     // optional
     $editUser->save();
     $admin->attachPermission($createPost);
     // equivalent to $admin->perms()->sync(array($createPost->id));
     $owner->attachPermissions(array($createPost, $editUser));
 }
開發者ID:cdandy,項目名稱:meta-admin,代碼行數:35,代碼來源:RoleController.php


注:本文中的app\models\Role::findOrFail方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。