mysql - SQLSTATE[HY000] : General error: 1364 Field 'name' doesn't have a default value

标签 mysql laravel

我正在使用mysql v5.7laravel 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();
        });
    }

我的ProductControllerstore方法

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/

相关文章:

mysql - 比全文搜索对资源使用更友好的搜索?

c# - fluent nhibernate 的大问题,c# 和 MySQL 需要在 BLOB 中搜索

php - Laravel 错误 : SQLSTATE[42S02]: Base table or view not found

PHP Redis session 处理程序多用户

php - Laravel Carbon 数据丢失

MySQL - 不同无关表之间的单个查询与多个查询

php - 单个 SQL 查询从具有多对多关系的表中获取结果

javascript - Laravel:更改文本框编辑上的元素

javascript - Uncaught ReferenceError : $ is not defined at http://localhost/project/public/user/1/edit:308:9 in Laravel

Mysql从连接结果中删除