本文整理汇总了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]);
}
示例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);
}
示例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]);
}
示例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;
}
示例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');
}
示例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();
}
示例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();
}
示例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);
}
示例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();
}
示例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);
}
示例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);
}
示例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;
}
}
示例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]);
}
示例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');
}
示例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));
}