我正在使用mysql v5.7和laravel 5.5。当我尝试“添加产品”时,出现以下错误。
SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value
我的产品表格
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('description');
$table->string('size');
$table->string('price');
$table->string('image');
$table->timestamps();
});
}
我的ProductController与store方法
public function store(Request $request)
{
//validation
$this->validate($request,[
'name'=> 'required',
'description'=>'required',
'size'=>'required',
'price'=>'required',
'image'=>'image|mimes:png,jpg,jpeg|max:10000'
]);
//image upload
$image=$request->image;
if($image){
$imageName=$image->getClientOriginalName();
$image->move('images',$imageName);
$formInput['image']=$imageName;
}
Product::create($formInput);
return redirect()->route('product.index');
}
当我向字段添加->nullable
方法时,没有错误,但表中的数据为空。
请帮助我...
最佳答案
您从未将经过验证的数据定义为变量。尝试此方法进行验证。
$formInput = $this->validate($request,[
'name'=> 'required',
'description'=>'required',
'size'=>'required',
'price'=>'required',
'image'=>'image|mimes:png,jpg,jpeg|max:10000'
]);
这会将变量 $formInput
中的所有已验证字段存储为数组。
关于mysql - SQLSTATE[HY000] : General error: 1364 Field 'name' doesn't have a default value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50260116/