Reputation: 609
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
:
ProductsController.php
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'));
}
Upvotes: 0
Views: 2743
Reputation: 473
The main problem you have here is that in your migration you have category_id is not nullable which means it should have a value assigned to it from the controller you can either assign a value for it from your controller or you can go to your migration file and add this line
$table->integer('catagory_id')->nullable();
Then you will remigrate the table and it shouldn't throw this error again, but I prefer that you assign a value for it if you have relationship or something you want to connect it to it but if you don't have relationships then the entire category_id
is useless in my point of view
If you want to assign a value to it, you should do something like this in your controller after the new product line:
$product->category_id = $request->input('category_id');
because your controller didn't take the value of the input which has the name of
category_id
Upvotes: 1
Reputation: 141
Although, there would be better ways to write this problem, the immediate solution would be to set the category_id field of your Product.
$product->product_name = $data['product_name'];
$product->product_code = $data['product_code'];
$product->product_color = $data['product_color'];
// Add this:
$product->category_id = $data['category_id'];
Upvotes: 2
Reputation: 1771
As the error implies, it does not have a default value, you could solve it by either create a migration (or revise the one you had) so the column 'category_id' is nullable or just add the following line in the product creation:
$product->category_id = NULL;
You may find more information if needed about nullable at Laravel migration documentation
Upvotes: -2