Skip to content
Advertisement

SQLSTATE[23000]: Integrity constraint violation: 1048 Column ‘feature_item’ & ”status’ cannot be null

in my form when i checked the checkedbox there is no error .but when i don’t want to check the checkedbox, I am getting the error SQLSTATE[23000]: 1048 Column ‘feature_item’ & ”status’ cannot be null.

1.my products table:-

public function up()
    {
      Schema::create('products', function (Blueprint $table) {
           $table->increments('id');
           $table->integer('category_id');
           $table->string('product_name');
           $table->string('product_code');
           $table->string('product_color');
           $table->text('description'); 
           $table->boolean('feature_item')->default(0);
           $table->boolean('status')->default(0);
           $table->timestamps();
       });
   }

2.my product.blade.php This is my form

<div class="control-group">
 <label class="control-label">Feature Item</label>
 <div class="controls">
 <input type="checkbox" name="feature_item" id="feature_item" value="1">
</div>
</div>


<div class="control-group">
<label class="control-label">Status</label>
<div class="controls">
<input type="checkbox" name="status" id="status" value="1">
</div>
</div>

3.my ProductsController

public function addproduct(Request $request){

         $product->category_id=$request['category_id'];
         $product->product_name=$request['product_name'];
         $product->product_code=$request['product_code'];
         $product->product_color=$request['product_color'];
         $product->description=$request['description'];
         $product->feature_item=$request['feature_item'];
         $product->status=$request['status'];

         if (!empty($request['description'])) {
             $product->description = $request['description'];
         } else {

             $product->description = '';
         }

         if(empty($request['feature_item'])){
             $feature_item='0';
         }else{
             $feature_item='1';
         }

         $product->status=$request['status'];
         if (empty($request['status'])) {
             $status = '0';
         } else {
             $status = '1';
         }

         $product->save();
         return back()->with('success','product Upload Successfully!');
         }

This error I get:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column ‘feature_item’ & ‘status’ cannot be null

Any help ps ?

Advertisement

Answer

In your controller, you check if status and feature_item are empty but you don’t define, what should be written to your model in that case, you’re only assigning values to variables, that will never be used again.

To fix that, assign those values to the product properties to ensure, they get saved.

if (empty($request['feature_item'])){
    $product->feature_item = '0';
} else {
    $product->feature_item = '1';
}

if (empty($request['status'])) {
    $product->status = '0';
} else {
    $product->status = '1';
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement