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

Issue

I’m working on a family project, not really a good coder, i used an already made app to edit myself, I added multiple columns to my Products Table as a migration :

public function up()
    {
        Schema::table('products', function (Blueprint $table) {
            //
            $table->integer('N_serie')->after('stock');
            $table->integer('quantite_fabrique')->after('N_serie');
            $table->unsignedDecimal('prix_qtt_fabrique', 10, 2)->after('quantite_fabrique');
            $table->integer('unite_lg')->after('prix_qtt_fabrique')->nullable();
            $table->integer('qtt_fabr_par_unit')->after('unite_lg')->nullable();
            $table->unsignedDecimal('prix_unite', 10, 2)->after('qtt_fabr_par_unit');
            $table->unsignedDecimal('prix_par_kg', 10, 2)->after('prix_unite');
            $table->unsignedDecimal('prix_1', 10, 2)->after('prix_par_kg')->nullable();
            $table->unsignedDecimal('prix_45', 10, 2)->after('prix_1')->nullable();
            $table->unsignedDecimal('prix_total', 10, 2)->after('prix_45')->nullable();
            $table->unsignedDecimal('comis_commerciale', 10, 2)->after('prix_total');
            $table->unsignedDecimal('prix_gros', 10, 2)->after('comis_commerciale');
        });
    }

I changed the fillable variable, and appended these to the old one

    protected $fillable = [
            'name', 'description', 'product_category_id', 'price', 'stock','N_serie', 'quantite_fabrique','prix_qtt_fabrique','unite_lg','qtt_fabr_par_unit','prix_unite','prix_par_kg','prix_1','prix_45','prix_total','comis_commerciale','prix_gros','stock_defective'
        ];

I went to my view and added the new columns to the form, to be added, this is an example of one column code : 

   <div class="form-group{{ $errors->has('N_serie') ? ' has-danger' : '' }}">
                                    <label class="form-control-label" for="input-name">N° de Série</label>
                                    <input type="text" name="name" id="input-name" class="form-control form-control-alternative{{ $errors->has('N_serie') ? ' is-invalid' : '' }}" placeholder="N° de Série" value="{{ old('N_serie') }}" required autofocus>
                                    @include('alerts.feedback', ['field' => 'N_serie'])
                                </div>

When i hit the save button i get This error:

SQLSTATE[HY000]: General error: 1364 Field 'quantite_fabrique' doesn't have a default value (SQL: insert into products (price, category_id, updated_at, created_at) values (20, 3, 2020-12-11 07:00:34, 2020-12-11 07:00:34))

and this is my controller function that saves all :

public function store(ProductRequest $request, Product $model)
    {
        $model->create($request->all());

        return redirect()
            ->route('products.index')
            ->withStatus('Product successfully registered.');
    }

i don’t know where the problem is, please Help

Solution

You have quantite_fabrique column set as not null. You should fill that field. Since you are using ProductRequest you can use

public function store(ProductRequest $request)
    {
        if(Product::create($request->validated())){
          return redirect()
            ->route('products.index')
            ->withStatus('Product successfully registered.');
        }

        return redirect()
            ->route('products.index'); // throw your error here
    }

Also, Your ProductRequest has to have ‘quantite_fabrique’ field as to be validated field.

Answered By – gguney

Answer Checked By – Mary Flores (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.