SQLSTATE [HY000]:常规错误:1364字段“照片”在laravel 5.5

时间:2017-09-20 13:00:31

标签: php mysql laravel-5.5

我正在尝试在注册期间上传用户照片,但我收到此错误,我在网上搜索并创建了可能的解决方案对我不起作用。我正在使用sentinel包进行高级身份验证。请有人帮帮我。 这是我的控制器 -

 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

命名空间App;

使用Illuminate \ Notifications \ Notifiable; 使用Illuminate \ Foundation \ Auth \ User作为Authenticatable;

类用户扩展Authenticatable {     使用通知;

/**
 * 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');
    });

3 个答案:

答案 0 :(得分:2)

  

常规错误:1364字段“照片”没有默认值

此错误表示关联表中的列photo没有默认值,并且您不在插入查询中包含该列。

要解决此问题,请将该列NULLABLE或传递一些值插入该列。

答案 1 :(得分:0)

你的问题在这里

$request['photo'] = $safeName

您无法直接在Request Class Object中添加关联数组等值 这就是photo没有价值的原因,它为SQL插件提供了错误。

你应该做这样的事情

$request->request->add(['photo' => $safeName]);

或更好的方式

$user = Sentinel::registerAndActivate(array_merge($request->all(),['photo' => $safeName]));

答案 2 :(得分:0)

请检查你的mysql版本,如果你有超过5.6.39那么。请转到变量并找到sql模式并删除STRICT_TRANS_TABLES enter image description here