$ user-> getAuthPassword()和Auth :: user() - >密码之间有什么大不同?laravel 4

Dor*_*ora 1 php authentication passwords if-statement laravel

我正在网上学习这个登录教程,选择用户在登录后更改密码并给出注释

if ($validator->fails())
{
    return Redirect::route('account-change-password')
        ->withErrors($validator);
}
else
{
    $user = User::find(Auth::user()->id);

    $old_password = Input::get('old_password');
    $password = Input::get('password');

    if(Hash::check($old_password, $user->getAuthPassword()))
    {
        $user->password = Hash::make($password);

        if($user->save())
        {
            return Redirect::route('home')
                ->with('global', 'Your Password has been changed');
        }
    }
    else
    {
        return Redirect::route('account-change-password')
            ->with('global', 'Your old password is incorrect');
    }
}
Run Code Online (Sandbox Code Playgroud)

在else中的教程$user = User::find(Auth::user()->id);中立即使用,然后检查密码的if语句正在使用$user = User::find(Auth::user()->id);

当我在看完教程后尝试在我的上面做的时候我做了不同的事情.

if ($v->fails())
{
    return Redirect::route('account-change-password')
        ->withErrors($v)
        ->with('global', 'Please check the errors in red');
}
else
{
    if (Hash::check(Input::get('old_password'),Auth::user()->password))
    {
        $user = User::find(Auth::user()->id);
        $user->password = Hash::make(Input::get('new_password'));
        if ($user->save())
        {
            return Redirect::route('home')
                ->with('global', 'Password Changed.');
        }
    }
    return Redirect::route('account-change-password')
            ->with('global', 'Old password is incorrect');
}
Run Code Online (Sandbox Code Playgroud)

我立即Auth::user()->password)使用if语句检查了数据库中的密码和密码,并将if语句放在$user = User::find(Auth::user()->id);内部,以便我可以访问$user->password并更改密码.

我知道总有很多不同的方法可以做,但我只是想知道我这样做的方式将来是否会出现某种问题,我现在还没有意识到这一点,最好记住并坚持下去.教程教导的方式.

The*_*pha 7

实际上,你应该查看源代码,它在你的范围内,所以当你使用下面的代码时,为什么不深入挖掘呢?

$user = User::find('1');
$user->getAuthPassword();
Run Code Online (Sandbox Code Playgroud)

此代码返回当前实例化用户的密码,因此它是用户,id=1可能是您可能想要的任何用户,取决于您,从数据库中检索谁.getAuthPassword函数内的代码是:

public function getAuthPassword()
{
    return $this->password;
}
Run Code Online (Sandbox Code Playgroud)

因此,从数据库检索的用户是否已登录并不重要.如果您检索用户,id=10那么您也可以获得此用户的密码.

另一方面,以下代码:

Auth::user()->password;
Run Code Online (Sandbox Code Playgroud)

它返回当前登录用户的密码,因为它Auth::user()返回当前登录的用户实例,此方法仅在当前用户登录时才有效.所以,是的,两种情况都有区别.