If you view the source of the page in the browser is your product_id being set properly here?
<input type="hidden" name="product_id" value="{{ $productDetails->product_id }}">
Maybe it's not being sent properly by your controller?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
m trying to add to cart a product nut when i click on add to cart button on detail.blade.php page then it shows this error: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'product_id' cannot be null (SQL: insert into cart (product_id, product_name, product_code, product_color, price, size, quantity, user_email, session_id) values (, Filter, ASD111, White, 5000, Choose an option, 1, , ))
any solution to resolve this issue
here is detail.blade.php:
<form name="addtocartForm" id="addtocartForm" action="{{ url('add-cart') }}"
method="post">
{{ csrf_field() }}
<input type="hidden" name="product_id" value="{{ $productDetails->product_id }}">
<input type="hidden" name="product_name" value="{{ $productDetails-
>product_name }}">
<input type="hidden" name="product_code" value="{{ $productDetails->product_code
}}">
<input type="hidden" name="product_color" value="{{ $productDetails-
>product_color }}">
<input type="hidden" name="price" value="{{ $productDetails->price }}">
<div class="w-size14 p-t-30 respon5">
<h4 class="product-detail-name m-text16 p-b-13">
{{ $productDetails->product_name }}
</h4>
<span class="m-text17">
PKR: {{ $productDetails->price }}
</span>
</form>
code of ProductsController:
public function addtocart(Request $request){
$data = $request->all();
//echo "<pre>"; print_r($data); die;
if(empty($data['user_email'])){
$data['user_email'] = '';
}
if(empty($data['session_id'])){
$data['session_id'] = '';
}
DB::table('cart')->insert(['product_id'=>$data['product_id'],
'product_name'=>$data['product_name'],
'product_code'=>$data['product_code'],
'product_color'=>$data['product_color'],
'price'=>$data['price'],
'size'=>$data['size'],'quantity'=>$data['quantity'],
'user_email'=>$data['user_email'],
'session_id'=>$data['session_id']]);
}
route:
Route::match(['get','post'], '/add-cart','ProductsController@addtocart');
Please or to participate in this conversation.