1👍
Put your commit
method outside the last if
condition.
public function store(ProductSaveRequest $request)
{
DB::beginTransaction();
$product = new Product;
$product->fill($request->all());
$file = $request->file('image');
if ($file) {
$file = $request->file('image');
$filename = Uuid::uuid() . '.' . $file->getClientOriginalExtension();
$file->move('img/products', $filename);
$product->image = 'img/products/' . $filename;
}
$product->save();
Log::info($product);
if (!empty($request->get('prices'))) {
$prices = array_map(function ($item) use ($product) {
$item['product_id'] = $product->id;
return $item;
}, $request->get('prices'));
CountryProduct::insert($prices);
}
if ($request->has('tags')) {
$product->tags()->sync($request->get('tags'));
}
if ($request->has('sizes')) {
$product->sizes()->sync($request->get('sizes'));
}
if ($request->has('categories')) {
$productCategories = [];
foreach ($request->get('categories') as $item) {
if (key_exists('category_name', $item)) {
$category = Category::where('name', $item['category_name'])
->first();
} else if (key_exists('category_id', $item)) {
$category = Category::findOrFail($item['category_id']);
}
if (empty($category)) {
$category = new Category;
$category->name = $item['category_name'];
}
$category->type = $item['type'];
$category->save();
if (!empty($item['fields'])) {
foreach ($item['fields'] as $fieldItem) {
if (key_exists('field_name', $fieldItem)) {
$field = Field::where('name', $fieldItem['field_name'])
->first();
} else if (key_exists('field_id', $fieldItem)) {
$field = Field::findOrFail($fieldItem['field_id']);
}
if (empty($field)) {
$field = new Field;
$field->name = $fieldItem['field_name'];
$field->category_id = $category->id;
}
$field->save();
$productCategories[] = [
"field_id" => $field->id,
"category_id" => $category->id,
"product_id" => $product->id,
"value" => $fieldItem['value']
];
}
}
if (count($productCategories) > 0) {
ProductField::insert($productCategories);
}
ExportationFactor::insert($product);
}
}
DB::commit();
return $product;
}
0👍
In the first line of the method, you begin a database transaction.
But you only commit the database transaction
if ($request->has('categories')) {
- [Vuejs]-(Laravel 5.6, vue) Axios 401 error with PUT,POST,PATCH on Safari
- [Vuejs]-How do I populate auto generated input fields with Vue / Laravel
Source:stackexchange.com