doushouhe7072 2018-09-20 13: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 13: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条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制