Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Kanchan186's avatar

Illuminate \ Database \ QueryException (HY000) SQLSTATE[HY000]: General error: 1364 Field 'p_id' doesn't have a default value (SQL: insert into `prod_cats` (`updated_at`, `created_at`) values (2019-09-03 05:18:24, 2019-09-03 05:18:24))

ProdCatController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\prod_cat;
use App\product;
use App\category;



class ProdCatController extends ProductController
{
   public function store1(Request $req)
    {
        //dd($req->all());
        prod_cat::create([
                    'p_id'=>request('p_id'),
                    'c_id'=>request('c_id'),

                    

                   ]);

    
         $product=product::get();
         $category=category::get();
         $prod_cat=prod_cat::get();

         array($request->get('c_id'));


        return view('pcview',compact('product','category','prod_cat'));
    }



    public function show1()
                 {

                    

                    $product=product::get();
                    $category=category::get();
                     $prod_cat=prod_cat::get();


                    return view('pcview',compact('product','category','prod_cat'));

                    //return redirect('prodview');
                  } 



}

prodview.blade.php

  <body>
        <div class="flex-center position-ref full-height">
            

            <div class="content">
               
                <form method="post" action="{{url('/')}}/store1" enctype="multipart/form-data">
                        {{csrf_field()}}
                <input type="hidden" name="MAX_FILE_SIZE" value="10485760">


                     <table  class="table table-striped">
                        
                   
                        <tr> <td><select name="p_id" class="form-control">
                             @foreach($product as $prod)
                             <option value ="{{$prod->p_id}}">{{$prod->p_name}}</option>
                             @endforeach
                             </select></td> 
                        </tr>

                        <tr><td>
                            @foreach($category as $cat)
                            <input type="checkbox" name="cat[{{$cat->c_id}}]"  value ="{{$cat->c_id}}">{{$cat->c_name}}<br>
                            @endforeach</td>

                        <tr><td><input type="submit" name=""></td></tr>


                       </table> </form><br><hr>
               
            </div>
        </body>

prod_cat.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class prod_cat extends Model
{
   protected $guarded = ['pc_name','p_id','c_id'];
    protected  $primaryKey = 'pc_id';
}
0 likes
9 replies
Nakov's avatar

In your prod_cat model add this:

protected $fillable = ['p_id', 'c_id'];

And remove the $guarded because $guarded means don't allow someone else to set the value of the fields in this array. You should either have $guarded as an empty array to allow all, or $fillable to allow specific fields to be set. So you are guarding at the moment from someone to set the p_id and c_id and yet you want to create a resource of those values in the controller, hence the exception.

Kanchan186's avatar

hello sir, when i make changes i got again an error

Illuminate \ Database \ QueryException (23000)
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'c_id' cannot be null (SQL: insert into `prod_cats` (`p_id`, `c_id`, `updated_at`, `created_at`) values (1, ?, 2019-09-03 06:04:28, 2019-09-03 06:04:28))

Nakov's avatar

@kanchan186 so the error says it all. In your view you don't have an item with name="c_id" and you are using request('c_id') in your controller which returns null so trying to store null is not allowed.

munazzil's avatar

Your using in the function as $req but in the create function your calling request change as like below and check,

   public function store1(Request $request)
            {
            //dd($req->all());
            prod_cat::create([
                'p_id'=>$request('p_id'),
                'c_id'=>$request('c_id'),
               ]);

            // other codes

            }
Kanchan186's avatar

i make above changes still getting same error

Illuminate \ Database \ QueryException (23000)
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'c_id' cannot be null (SQL: insert into `prod_cats` (`p_id`, `c_id`, `updated_at`, `created_at`) values (1, ?, 2019-09-03 06:19:25, 2019-09-03 06:19:25))
Kanchan186's avatar

i want to store multiple checkbox values in databases at a time. can u please check my array logic is wrong or right?

nolros's avatar

@kanchan186

   
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\prod_cat;
use App\product;
use App\category;

class ProdCatController extends ProductController
{
   public function store1(Request $request)
    {

        if ($request->p_id && $request->p_id ) {

        if ($categor = prod_cat::create([
                'p_id'=>$request->p_id,
                'c_id'=>$request->c_id
               ]);) {

            // remove the rest for brevity sake 

            return response('Product category created successfully', 200);

        }

        // or whatever you want to do when it fails
        return response('Failed to find ids', 422);
    }
}


1 like
Nakov's avatar
Nakov
Best Answer
Level 73

@kanchan186 please don't look at what @munazzil said, because that's completely the same, you were using the request helper function the error is not there.. Look at what I have said above, the problem is because you don't pass c_id to your controller which is null that's why you get the error.

This code:

@foreach($category as $cat)
    <input type="checkbox" name="cat[{{$cat->c_id}}]"  value ="{{$cat->c_id}}">{{$cat->c_name}}<br>
@endforeach</td>

Should be handled differently..

foreach(request('cat') as $cat_id) 
{
    prod_cat::create([
        'p_id'=> request('p_id'),
        'c_id'=> $cat_id,
    ]);
}
1 like
Kanchan186's avatar

Thank you so much sir.. error solved, i got my desired output

Please or to participate in this conversation.