1

在查看了新的 laravel 5.4 版后,我注意到使用“ $user = User::create(request(['name', 'email','password']));”密码时密码不会自动加密密码,是我,还是模型创建方法中默认哈希密码?我不记得了,但是不应该“创建”方法已经这样做了吗?

4

3 回答 3

3

在用户模型中,您需要添加以下功能,用于默认密码加密。

public function setPasswordAttribute($value)
        {
            if($value != ""){
                $this->attributes['password'] = bcrypt($value);
            }
        }
于 2017-01-31T12:22:21.880 回答
1

Laravel 文档中所述

如果您使用 Laravel 应用程序中包含的内置 LoginController 和 RegisterController 类,它们将自动使用 Bcrypt 进行注册和身份验证。

如果你使用RegisterController.phpLaravel 自带的,你不需要Hash手动输入密码,否则你需要使用

 return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']), //<==encrypt here
        ]);

在此处检查寄存器控制器:

https://github.com/laravel/laravel/blob/master/app/Http/Controllers/Auth/RegisterController.php#L63

于 2017-01-31T12:05:19.250 回答
1

试试下面的。

命名空间 App\Http\Controllers;

使用 Illuminate\Http\Request;

使用 Illuminate\Support\Facades\Hash;

使用应用\用户;

类 RegistrationsController 扩展控制器 {

public function store()
{
    //validate the form
    $this->validate(request(),[
        'name'=> ['required', 'string', 'max:255'],
        'email'=> ['required', 'string', 'email', 'max:255', 'unique:users'],
        'password'=>['required', 'string', 'min:4', 'confirmed'],
    ]);

    //create and save the user

        $user =User::create([
       'name'=>request('name'), 
       'email'=>request('email'),

        //hash your password 

       'password'=>Hash::make(request('password'))
       ]);

    //sign in the user
    auth()->login($user);

    //redirect to the homepage
     return redirect()->home();
}

}

于 2019-03-03T15:59:48.630 回答