1
votes

I am using mysql v5.7 and laravel 5.5. When I try to "add product" the following error occurs.

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

My products table

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

My ProductController with store method

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

When I add ->nullable method to the field, there is no error but the data in table is null.

Please help me...

2
Is $formInput ever defined? As far as I can see it only has one key, which is image.Classified

2 Answers

1
votes

You never defined the validated data as a variable. Try this for validation.

$formInput = $this->validate($request,[
    'name'=> 'required',
    'description'=>'required',
    'size'=>'required',
    'price'=>'required',
    'image'=>'image|mimes:png,jpg,jpeg|max:10000'
]);

This will store all the validated fields within the variable $formInput as an array.

1
votes

Please check if you have specified the name field in the $fillables in your model.

protected $fillables = [ 'name'];