amitlalvani's avatar

input file not giving empty if not selected image

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

0 likes
5 replies
Tray2's avatar

In a table

$table->id();
$table->int('quantity');
$table->decimal('price', $precision = 8, $scale = 2);
$table->string('product_image')->nullable();
$table->timestamps();
amitlalvani's avatar

anyloop

    <input type="number" name="price[]">
   <input type="number" name="qty[]">
   <input type="file" name="image[]">

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],
    ]);
    if (isset($images[$i]) && $images[$i]->isValid()) {
        $imagePath = $images[$i]->store('variation_images', 'public');
        $stock->product_image = $imagePath;
        $stock->save();
    } elseif (isset($emptyImages[$i])) {
       	  $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

2=>price 3

0=>qty 1

1=>qty 2

2=>qty 3

0=>image 1

1=>image 2

how should i store this

Snapey's avatar

you will find it much easier if you use array syntax on your form

   <input type="number" name="products[][price]">
   <input type="number" name="products[][qty]">
   <input type="file" name="products[][image]">

Now you will have an array of products, each will have three keys, and you will not need to use a counter to iterate over the input fields

Please or to participate in this conversation.