doushouhe7072 2018-09-20 05:04
浏览 203
已采纳

Laravel在没有密码输入的情况下提交表单时在数据库中插入随机密码

I want to insert random password in database when I submit a form without password input field.

my model User.php

protected $fillable = [
    'email', 'firstname', 'lastname'
];

public function setpasswordAttribute($value)
{
    $this->attributes['password'] = bcrypt($value ?: str_random(10));
}

my controller

public function store(Request $request)
{
    User::create(Request::all());
    return 'test';
}

my database

id
firstname
lastname
password
created_at
updated_at

my error

SQLSTATE[HY000]: General error: 1364 Field 'password' doesn't have a default value 
(SQL: insert into `users` (`email`, `firstname`, `lastname`, `updated_at`, `created_at`)
  • 写回答

4条回答 默认 最新

  • duanpo8329 2018-09-20 05:32
    关注

    SQLSTATE[HY000]: General error: 1364 Field 'password' doesn't have a default value (SQL: insert into users (email, firstname, lastname, updated_at, created_at)

    You are getting this error because the default create_users_table migration doesn't provide default value to the password field or allow null as value.

    when you call

    User::create($request->all()); 
    

    laravel perform an insert query to the database and because the password is not set, MySQL will return the `SQLSTATE[HY000]

    You can solve that by modifying the create_users_table migration which came by default with a new created laravel project,

      Schema::create('users', function (Blueprint $table) {
            // other table columns goes here
            $table->string('password')->nullable();
            // OR 
            $table->string('password')->default("");
            // other table columns goes here
      });
    

    this will allow you to create new User without provide a password and that column will be set to "" or left empty depending with the method you use to modify the migration like I suggest

    Secondly defyning the setPasswordAttribute on your model doesn't means when you create a new user without password it will set automaticaly a password on that, you can do that by catching the creating event before inserting the user in the database.

    to do that add this in you EventServiceProvider

    class EventServiceProvider extends ServiceProvider
    {
    
        /**
        * Register any events for your application.
        *
        * @return void
        */
        public function boot()
        {
            parent::boot();
            User::creating(function($user){
                if($user->password === "" || empty($user->password)){
                    $user->password  = bcrypt(str_random(10));;
                }
            })
    
        }
    }
    

    this event is trigger before the user is persiste in the database, and that will allow you to make some modification on all attributes that are allready set on the user you want to persiste in the database

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部