App\Modules\Users\Controllers\Admin\Users::update PHP Method

update() public method

public update ( $id )
    public function update($id)
    {
        // Get the User Model instance.
        $user = User::find($id);
        if ($user === null) {
            // There is no User with this ID.
            $status = __d('users', 'User not found: #{0}', $id);
            return Redirect::to('admin/users')->withStatus($status, 'danger');
        }
        // Validate the Input data.
        $input = Input::only('username', 'role', 'realname', 'password', 'password_confirmation', 'email');
        if (empty($input['password']) && empty($input['password_confirm'])) {
            unset($input['password']);
            unset($input['password_confirmation']);
        }
        $validator = $this->validate($input, $id);
        if ($validator->passes()) {
            $origName = $user->username;
            // Update the User Model instance.
            $user->username = $input['username'];
            $user->role_id = $input['role'];
            $user->realname = $input['realname'];
            $user->email = $input['email'];
            if (isset($input['password'])) {
                // Encrypt and add the given Password.
                $user->password = Hash::make($input['password']);
            }
            // Save the User information.
            $user->save();
            // Prepare the flash message.
            $status = __d('users', 'The User <b>{0}</b> was successfully updated.', $origName);
            return Redirect::to('admin/users')->withStatus($status);
        }
        // Errors occurred on Validation.
        $status = $validator->errors();
        return Redirect::back()->withInput()->withStatus($status, 'danger');
    }