Php 更新记录的用户密码laravel 5

Php 更新记录的用户密码laravel 5,php,laravel,laravel-5,laravel-5.1,Php,Laravel,Laravel 5,Laravel 5.1,我想为登录用户添加密码更新选项,因此我使用了以下代码 控制器auth\authController.php public function updatePassword() { $user = Auth::user(); $rules = array( 'old_password' => 'required', 'password' => 'required|alphaNum|between:6

我想为登录用户添加密码更新选项,因此我使用了以下代码

控制器auth\authController.php

public function updatePassword()
    {

        $user = Auth::user();
        $rules = array(
            'old_password' => 'required',
            'password' => 'required|alphaNum|between:6,16|confirmed'
        );

        $validator = Validator::make(Input::all(), $rules);

        if ($validator->fails()) {
            return Redirect::route('change-password', $user->id)->withErrors($validator);
        } else {
            if (!Hash::check(Input::get('old_password'), $user->password)) {
                return Redirect::route('change-password', $user->id)->withErrors('Your old password does not match');
            } else {
                $user->password = Input::get('password');
                $user->save();
                return Redirect::route('change-password', $user->id)->with("message", "Password have been changed");
            }
        }
    }
路线

Route::post('change-password', 'Auth\AuthController@updatePassword');
Route::get('change-password', 'Auth\AuthController@updatePassword');
我得到以下错误

FatalErrorException in AuthController.php line 123:
Class 'App\Http\Controllers\Auth\Auth' not found

对于此行“$user=Auth::user();”

的名称空间问题,请尝试:

//if this method is not protected by a middleware for only authenticated users
//verify that user is currently logged in:

if(!$user = \Auth::user()) abort(503); //or return redirect()->route('login')

$rules = array(
    'old_password' => 'required',
    'password' => 'required|alphaNum|between:6,16|confirmed'
);
或者在
AuthController

use Auth;

class AuthController{
   ....
}

您没有导入身份验证类

将此添加到文件的顶部。在名称空间之后


使用Illumb\Support\Facades\Auth

你的问题有隐藏的答案..我有类似的问题,比如@faz..我已经用他的问题代码做了这个把戏

实现这一目标的正确方法-

 protected function postChangePassword(ChangePasswordFormRequest $request){

    $user = Auth::user();
    $current_password = Input::get('current_password');
    $password = bcrypt(Input::get('password'));

    $user_count = DB::table('users')->where('id','=',$this->user_id)->count();

    if (Hash::check($current_password, $user->password) && $user_count == 1) {
        $user->password = $password;
        try {
            $user->save();
            $flag = TRUE;
        }
        catch(\Exception $e){
            $flag = FALSE;
        }
        if($flag){
            return redirect('/u/0/change/password')->with('success',"Password changed successfully.");
        }
        else{
            return redirect('/u/0/change/password')->with("danger","Unable to process request this time. Try again later");
        }
    }
    else{
        return redirect('/u/0/change/password')->with("warning","Your current password do not match our record");
    }
}

请注意,对于Hash和Auth,我们需要在顶部包含类和我通过构造函数获得的用户id
$this->user\u id=Auth::user()->id。我想我已经帮助了很多人。

我可以理解您的问题,您只需使用laravel的auth名称空间,就可以在控制器文件的顶部写下这一行

use Auth;

将解决您的问题。

由于Laravel的别名,您只需导入
Auth
。尽管如此,进口并不是绝对必要的;您可以直接调用
\Auth
。@dakine确定它已修复。感谢现在我在AuthController.php第138行中收到了ErrorException:尝试获取$user->id和$user->password的非对象错误属性此ErrorException意味着
$user
不是对象,这意味着用户当前未登录in@Digitlimit:好的,你知道为什么登录后它总是返回主页吗。即使我尝试访问更改密码,pagehome也会在重定向路径()中设置,确定其已修复。感谢现在我在AuthController.php第138行中得到了ErrorException:尝试获取$user->id和$user->password的非对象错误属性您可以添加以下内容:
如果(!$user=Auth::user())中止(404)
//或将用户重定向到登录页,或者您也可以使用中间件。如果您的authcontroller构造方法中有来宾中间件,则需要从来宾中间件中豁免
updatePassword
,例如:
$this->中间件('guest',['except'=>['updatePassword']]
@Digitlimit现在我收到重定向循环错误,但id正在通过url:(我可以看看您的AuthController代码吗?特别是您的_构造()