pro
pro

Reputation: 609

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

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

Upvotes: 1

Views: 4204

Answers (3)

Udhav Sarvaiya
Udhav Sarvaiya

Reputation: 10061

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

Upvotes: 0

Gabriel
Gabriel

Reputation: 970

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

Upvotes: 1

DevK
DevK

Reputation: 9942

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

Upvotes: 0

Related Questions