php - SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'color_code' cannot be null laravel
Get the solution ↓↓↓I have two tables products and colors.
I want to store multiple rows in multiple tables within my database. My controller is currently not storing multiple values, it is only saving one row in my products and item lists tables.
How to bet? If it was empty.
blade
<div class="row no-gutters mb-1">
<div class="col-md-3">
<label for="color_name" class="ml-1">color_name:</label>
<input type="text" id="color_name" name="color_name[]" class="form-control w-auto d-inline">
</div>
<div class="col-md-3">
<label for="color_code" class="ml-1">color_code:</label>
<input type="text" id="color_code" name="color_code[]" class="form-control w-75 d-inline jscolor">
</div>
<div class="col-md-1">
<a onclick="addColor()" class="btn btn-info"><i class="fas fa-plus"></i></a>
<a onclick="deleteColor()" class="btn btn-danger"><i class="fas fa-times"></i></a>
</div>
</div>
ProductController.php
public function store(ProductRequest $request)
{
$product = Product::create([
'title' => $request->title,
'latin' => $request->latin,
'price' => $request->price,
'discount' => $request->discount,
'body' => $request->body,
'keywords' => $request->keywords,
'descriptions' => $request->descriptions,
]);
$product->categories()->attach($request->category_id);
$colorCodes = $request->color_code;
$colorNames = $request->color_name;
foreach ($colorCodes as $key => $color_code) {
if ($request->color_code != null && $request->color_name != null) {
$color = new Color;
$color->product_id = $product->id;
$color->color_code = $color_code;
$color->color_name = $colorNames[$key];
$color->save();
}
}
return redirect()->route('products.index');
}
To determine if there are any results you can do any of the following:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'color_name' cannot be null (SQL: insert into
colors
(product_id
,color_code
,color_name
,updated_at
,created_at
) values (20, #FFFFFF, ?, 2021-07-29 18:38:17, 2021-07-29 18:38:17))
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: composer detected issues in your platform: your composer dependencies require a php version ">= 8.0.2".
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.