php - SQLSTATE[HY000] : General error: 1364 Field 'photo' doesn't have a default value in laravel 5. 5

标签 php mysql laravel-5.5

我试图在注册期间上传用户照片,但出现此错误,我在网上搜索并发现可能的解决方案对我不起作用。我正在使用哨兵包进行高级身份验证。请任何人帮助我。 这是我的 Controller -

 public function postRegister(Request $request){
    $request->validate([
        'first_name' => 'required|max:255',
        'last_name' => 'required|max:255',
        'user_name' => 'unique:users|max:255',
        'email' => 'required|unique:users|email|max:255',
        'password' => 'required|min:6|confirmed',
        'phone' => 'required|numeric',
    ]);
    //upload image
    if ($file = $request->file('photo')) {
        $fileName = $file->getClientOriginalName();
        $extension = $file->getClientOriginalExtension() ?: 'png';
        $folderName = '/uploads/users/';
        $destinationPath = public_path() . $folderName;
        $safeName = str_random(10) . '.' . $extension;
        $file->move($destinationPath, $safeName);
        $request['photo'] = $safeName;
    }

    // $user = Sentinel::registerAndActivate($request->all());
    $user = Sentinel::registerAndActivate($request->all());
    // dd($user);
    //$activation = Activation::create($user);

    $role = Sentinel::findRoleBySlug('member');

    $role->users()->attach($user);
    //$this->sendEmail($user, $activation->code);
    //return redirect('/login');
    return ('You have seccsessfully registered. Now login to start');
}

和表单输入-(enctype="multipart/form-data"在表单顶部)

 <div class="row">
    <div class="col-md-6">
      <div class="sponsor-row">
        <label for="sponsor_input"><i class="fa fa-tags"></i></label>
        <input type="text" name="sponsor" id="sponsor_input" class="sponsor-input" placeholder="Sponsor"></input>
      </div>
    </div>
    <div class="col-md-6">
      <div class="photo-row">
        <label for="photo_input"></label>
        <input type="file" name="photo" id="photo" class="photo-input"></input>
      </div>
    </div>
  </div>
</div>

我的用户模型是:

<?php

命名空间应用;

使用 Illuminate\Notifications\Notifiable; 使用 Illuminate\Foundation\Auth\User 作为可验证的;

类用户扩展可验证 { 使用可通知的;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'first_name', 'last_name', 'user_name','email', 'password', 'phone', 'location', 'sponsor', 'photo',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];

我的迁移代码是:

Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('first_name')->nullable();
        $table->string('last_name')->nullable();
        $table->string('user_name');
        $table->string('email');
        $table->string('password');
        $table->string('phone');
        $table->string('location');
        $table->string('sponsor');
        $table->binary('photo')->nullable();
        $table->text('permissions')->nullable();
        $table->timestamp('last_login')->nullable();
        $table->timestamps();

        $table->engine = 'InnoDB';
        $table->unique('email');
    });

最佳答案

General error: 1364 Field 'photo' doesn't have a default value

此错误意味着关联表中有一列 photo 没有默认值,并且您没有在插入查询中包括该列。

要解决这个问题,要么将该列设置为NULLABLE,要么传递一些值以插入到该列中。

关于php - SQLSTATE[HY000] : General error: 1364 Field 'photo' doesn't have a default value in laravel 5. 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46322806/

相关文章:

php - 找不到类 'PragmaRX\Tracker\Vendor\Laravel\ServiceProvider'

mysql - 自动将 0 或空字符串转换为 NULL

php - laravel 5.5在构造函数中获取用户详细信息

php - 在 Laravel 5.5 中使用 JQuery AJAX 填充表格

php - 找不到类 'App\Http\Controllers\Auth\User'

php - 使用 SQL 查询获取团队排名

php - 如何在没有引用的情况下复制对象?

mysql - 如何进行查询以返回按天分组的行数?

mysql - 如何选择直到总和达到某个值

php - 将文件同步到 iOS 设备保护使用 htaccess