![](/img/trans.png)
[英]SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value laravel 5.5
[英]SQLSTATE[HY000]: General error: 1364 Field 'photo' doesn't have a default value in laravel 5.5
我正在嘗試在注冊過程中上傳用戶照片,但出現此錯誤,我在網上搜索並發現可能的解決方案對我不起作用。 我正在使用哨兵軟件包進行高級身份驗證。 請任何人幫助我。 這是我的控制器-
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 \\ Foundation \\ Auth \\ User作為Authenticateable;
類User擴展Authenticateable {使用Notifiable;
/**
* 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');
});
如果您使用的是最新版本的laravel,請轉到config / databse.php,並連接mysql
數組:-
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
默認情況下,strict為true並將其設置為false。(mysql數組中的第二個鍵)希望對您有所幫助!
常規錯誤:1364字段“照片”沒有默認值
此錯誤意味着關聯表中有一列photo
沒有默認值,並且您沒有在插入查詢中包括該列。
要解決此問題,請將該列設為NULLABLE
或傳遞一些值以插入該列中。
你的問題在這里
$request['photo'] = $safeName
您不能直接將諸如關聯數組之Request Class Object
值放在Request Class Object
,這就是為什么photo
沒有值的原因,並且它給SQL插入帶來了錯誤。
你應該做這樣的事情
$request->request->add(['photo' => $safeName]);
或更好的方法
$user = Sentinel::registerAndActivate(array_merge($request->all(),['photo' => $safeName]));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.