app/Api/V1/Controllers/RoleController.php
/**
     * Update the specified role by id in storage.
     * also sync the permissions in the request.
     * $request->permissions will be an array of permissions
     *
     * @param  \Illuminate\Http\Request $request
     * @param  string $role_name
     * @return json
     * @throws \Illuminate\Validation\ValidationException
     */
Update the specified role by id in storage.
also sync the permissions in the request.
$request->permissions will be an array of permissions


Update the specified role by id in storage . also sync the permissions in the request . $request - > permissions will be an array of permissions
public function update(Request $request, $role_name)
    {
        try {
            $role = Role::findByName($role_name);
        } catch (Exception $e) {
            throw new NotFoundHttpException('Role not found');
        }
        $this->validate($request, ['name' => 'required']);
        if(Role::query()->where('name', $request->name)->where('name', '!=', $role->name)->count()) {
            return $this->withMessage('Role name is already taken.', 400);
        }
        if ($role->isSpecialRole() || in_array($request->name, Role::SPECIAL_ROLES)) {
            if ($role->name === Role::SYSADMIN) {
                $role->syncPermissions(Permission::all());
            }
            throw new StoreResourceFailedException('This role can\'t be updated');
        }
        $permissions = $request->get('permissions', []);
        if (!Permission::areValidPermissions($permissions)) {
            throw new StoreResourceFailedException('We don\'t recognize some of the permissions you set');
        }
        if (!$role->canNotBeDeleted() && !in_array($request->name, Role::SPECIAL_ROLES)) {
            $role->name = $request->name;
        }
        if ($role->save()) {
            $role = Role::findByName($role->name);
            $role->syncPermissions($permissions);
            return $this->withJson(['message' => 'Roles & Permissions updated'], 201);
        }
        throw new StoreResourceFailedException('Role update failed');
    }
public function update ( Request $ request , $ role_name ) { try { $ role = Role :: findByName ( $ role_name ) ; } catch ( Exception $ e ) { throw new NotFoundHttpException ( 'Role not found' ) ; } $ this -> validate ( $ request , [ 'name' => 'required' ] ) ; if ( Role :: query ( ) -> where ( 'name' , $ request -> name ) -> where ( 'name' , '!=' , $ role -> name ) -> count ( ) ) { return $ this -> withMessage ( 'Role name is already taken.' , 400 ) ; } if ( $ role -> isSpecialRole ( ) || in_array ( $ request -> name , Role :: SPECIAL_ROLES ) ) { if ( $ role -> name === Role :: SYSADMIN ) { $ role -> syncPermissions ( Permission :: all ( ) ) ; } throw new StoreResourceFailedException ( 'This role can\'t be updated' ) ; } $ permissions = $ request -> get ( 'permissions' , [ ] ) ; if ( ! Permission :: areValidPermissions ( $ permissions ) ) { throw new StoreResourceFailedException ( 'We don\'t recognize some of the permissions you set' ) ; } if ( ! $ role -> canNotBeDeleted ( ) && ! in_array ( $ request -> name , Role :: SPECIAL_ROLES ) ) { $ role -> name = $ request -> name ; } if ( $ role -> save ( ) ) { $ role = Role :: findByName ( $ role -> name ) ; $ role -> syncPermissions ( $ permissions ) ; return $ this -> withJson ( [ 'message' => 'Roles & Permissions updated' ] , 201 ) ; } throw new StoreResourceFailedException ( 'Role update failed' ) ; }
Update the specified role by id in storage.
Update the specified role by id in storage .
// admin role has everything /* if ($role->name === 'admin') {
            $role->syncPermissions(Permission::all());
            return new Response(['status'=>  'Permissions updated'], 201);
        } */ //some roles can't be updated /* $permisssions = $permission->pluck('name')->toArray();
        return $this->response->array(array_intersect($permissions, $permisssions)); */ //verify permissions to make sure selected permissions are in db //some role names cannot be changed //save the modified role name //save the modified permissions for the role