1

I am trying to insert data into database but it says:

SQLSTATE[HY000]: General error: 1364 Field 'category_name' doesn't have a default value (SQL: insert into categories (updated_at, created_at) values (2019-07-23 02:34:10, 2019-07-23 02:34:10))

My controller:

  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();
  }

My model:

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

This is a Database table: enter image description here

Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64
pro
  • 609
  • 4
  • 17
  • 40

3 Answers3

1

There are spelling mistakes in your model as per your controller. You have to change your model to

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

If you use database migration, you have to update all the columns like:

       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();
        });

While designing your database, be very precise else use 'nullable' in columns which you are unsure if they are mandatory. Nullable declaration in migration is:

       $table->string('category_name')->nullable();
Gabriel
  • 970
  • 7
  • 20
0

categry_name in your $fillable is misspelt. It's missing an e

DevK
  • 9,597
  • 2
  • 26
  • 48
0

According to your controller, your model has spelling mistakes

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
}

More details: What does “Mass Assignment” mean in Laravel

Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64