SQLSTATE [HY000]:常规错误:1364字段“ category_name”没有默认值

时间:2019-07-23 02:42:06

标签: laravel eloquent laravel-5.8

我正在尝试将数据插入数据库,但是它说:

  

SQLSTATE [HY000]:常规错误:1364字段'category_name'没有   具有默认值(SQL:插入categoriesupdated_at中,   created_at)值(2019-07-23 02:34:10,2019-07-23 02:34:10))

我的控制器:

  public function store(Request $request)
  {
    //dd($request->all());
    $request->validate([
        'category_name'         => 'required',
        'category_description'  => 'required',
        'category_slug'         => 'required',
        'category_image'        => 'required|image',
    ]);

    $path = $request->file('category_image');
    $image = $path->getClientOriginalName();
    $path->move(public_path('images/backend_images/category_images'));

    $category = Category::create($request->all());

    return redirect()->back();
  }

我的模型:

   protected $fillable = [
     'categry_name', 'categry_description', 'categry_slug', 'categry_image'
   ];

这是数据库表: enter image description here

3 个答案:

答案 0 :(得分:1)

根据您的控制器,模型中存在拼写错误。 您必须将模型更改为

   protected $fillable = [
     'category_name', 'category_description', 'category_slug', 'category_image'
   ];

如果使用数据库迁移,则必须更新所有列,例如:

       Schema::create('categories', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('category_name');
            $table->string('category_description');
            $table->string('category_slug');
            $table->string('category_image');
            $table->timestamps();
        });

在设计数据库时,请非常精确,否则请在不确定的列中使用“ nullable ”。迁移中的空声明为:

       $table->string('category_name')->nullable();

答案 1 :(得分:0)

categry_name中的

$fillable拼写错误。它缺少e

答案 2 :(得分:0)

根据您的控制器,您的模型存在拼写错误

class categories extends Model {
          protected $fillable = ['category_name', 'category_description', 'category_slug', 'category_image']; 
          //only the field names inside the array can be mass-assign
}

更多详细信息:What does “Mass Assignment” mean in Laravel