I think it has something to do with the fillable in your Product model.
Try to add this in your Product.php:
protected $fillable = [
'category_id'
]
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I'm new to Laravel and trying to add Product under Category but when I add Product then it shows this error:
SQLSTATE[HY000]: General error: 1364 Field 'category_id' doesn't have a default value (SQL: insert into products ..."
Initially i was adding these products without under any category than it was working and now its not adding under Category.
can anyone would prefer to provide me its solution?
here is my form:
<form enctype="multipart/form-data" class="form-horizontal" method="post" action="{{
url('admin/add-product') }}" name="add_product" id="add_product"
novalidate="novalidate">{{ csrf_field() }}
<div class="control-group">
<label class="control-label">Under Category</label>
<div class="controls">
<select name="category_id" id="category_id" style="width:220px;">
<?php echo $categories_drop_down; ?>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label">Product Name</label>
<div class="controls">
<input type="text" name="product_name" id="product_name">
</div>
</div>
<div class="uploader" id="uniform-undefined"><input name="image" id="image" type="file"
size="19" style="opacity: 0;"><span class="filename">No file selected</span><span
class="action">Choose File</span></div>
div class="form-actions">
<input type="submit" value="Add Product" class="btn btn-success">
</div>
</form>
here is ProductsController:
public function addProduct(Request $request)
{
if ($request->isMethod('post'))
{
$data = $request->all();
$product = new Product;
$product->product_name = $data['product_name'];
$product->product_code = $data['product_code'];
$product->product_color = $data['product_color'];
if ( ! empty($data['description']))
{
$product->description = $data['description'];
}
else
{
$product->description = '';
}
$product->price = $data['price'];
// Upload Image
if ($request->hasFile('image'))
{
$image_tmp = Input::file('image');
if ($image_tmp->isValid())
{
$extension = $image_tmp->getClientOriginalExtension();
$filename = rand(111, 99999) . '.' . $extension;
$large_image_path = 'images/backend_images/products/large/' . $filename;
$medium_image_path = 'images/backend_images/products/medium/' . $filename;
$small_image_path = 'images/backend_images/products/small/' . $filename;
// Resize Images
Image::make($image_tmp)->save($large_image_path);
Image::make($image_tmp)->resize(600, 600)->save($medium_image_path);
Image::make($image_tmp)->resize(300, 300)->save($small_image_path);
// Store image name in products table
$product->image = $filename;
}
}
$product->save();
/*return redirect()->back()->with('flash_message_success','Product has been added
successfully!');*/
return redirect('/admin/view-products')->with('flash_message_success', 'Product has been
added successfully!');
}
$categories = Category::where(['parent_id' => 0])->get();
$categories_drop_down = "<option value='' selected disabled>Select</option>";
foreach ($categories as $cat)
{
$categories_drop_down .= "<option value='" . $cat->id . "'>" . $cat->name . "</option>";
$sub_categories = Category::where(['parent_id' => $cat->id])->get();
foreach ($sub_categories as $sub_cat)
{
$categories_drop_down .= "<option value='" . $sub_cat->id . "'> -- " .
$sub_cat->name . "</option>";
}
}
return view('admin.products.add_product')->with(compact('categories_drop_down'));
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
public function products(){
return $this->hasMany('App\Product');
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
public function category(){
return $this->belongsTo('App\Category');
}
}
public function addProduct(Request $request)
{
if ($request->isMethod('post'))
{
$data = $request->all();
$category = \App\Category::findOrFail($data['category_id']); //<<< NEW LINE
$product = new Product;
$product->product_name = $data['product_name'];
$product->product_code = $data['product_code'];
$product->product_color = $data['product_color'];
if ( ! empty($data['description']))
{
$product->description = $data['description'];
}
else
{
$product->description = '';
}
$product->price = $data['price'];
// Upload Image
if ($request->hasFile('image'))
{
$image_tmp = Input::file('image');
if ($image_tmp->isValid())
{
$extension = $image_tmp->getClientOriginalExtension();
$filename = rand(111, 99999) . '.' . $extension;
$large_image_path = 'images/backend_images/products/large/' . $filename;
$medium_image_path = 'images/backend_images/products/medium/' . $filename;
$small_image_path = 'images/backend_images/products/small/' . $filename;
// Resize Images
Image::make($image_tmp)->save($large_image_path);
Image::make($image_tmp)->resize(600, 600)->save($medium_image_path);
Image::make($image_tmp)->resize(300, 300)->save($small_image_path);
// Store image name in products table
$product->image = $filename;
}
}
// $product->save(); <<< comment this line
$category->products()->save($product);
//[...]
Please or to participate in this conversation.