In a table
$table->id();
$table->int('quantity');
$table->decimal('price', $precision = 8, $scale = 2);
$table->string('product_image')->nullable();
$table->timestamps();
anyloop loopend
in this suppose i have three value of price and qty and only 2 of image
so the problem is that input file has only array of two and price and qty have array of 3
so when i retrive it from request i am storing like this
for ($i = 0; $i < count($prices); $i++) { if ($quantities[$i] && $prices[$i]) { $stock = Stock::create([
'qty' => $quantities[$i],
'price' => $prices[$i],
]);
// Handle image upload for variations
if (isset($images[$i]) && $images[$i]->isValid()) {
$imagePath = $images[$i]->store('variation_images', 'public');
$stock->product_image = $imagePath;
$stock->save();
} elseif (isset($emptyImages[$i])) {
// If no new image is uploaded, fetch the existing image path from the database
$existingStock = Stock::where('product_id', $product->id)
->where('colour_id', $colIds[$i])
->where('size_id', $sizeIds[$i])
->first();
if ($existingStock) {
$stock->product_image = $existingStock->product_image;
$stock->save();
}
}
}
}
now in this i am getting
0=>price 1 1=>price 2 3=>price 3
0=>qty 1 1=>qty 2 3=>qty 3
0=>image 1 1=>image 2
how should i store this
Please or to participate in this conversation.