From the image name in the field. In your loop deal with one at a time is my suggestion. How did you store, json or related table.
Edit multiple images
When updating multiple images how can I know which image user selected to edit it in database and unlink the old one then upload the new one ?
I store the image in array name="product_img[]"
@foreach ($part->images as $key=>$image)
<input type="file" name="part_img[]" accept=".png, .jpg, .jpeg ,gif,svg" value="{{ $image->id }}" />
@endforeach
You will need to setup a loop and handle what is existing versus what is new or different.
However, one thing I have done in the past, is have the images a separate edit and update operation. So if user wants to change one or more images, you are only dealing with that aspect. But that is just one way.
@jlrdw I'm handled it with the other loop after that loop that dealing with the new images only but my question is that how to know which image user select to edit it
This is the other one that dealing with the new images
@for ($i = ($part->images->count()); $i < App\Models\Part::ImgCount; $i++)
<input type="file" name="part_img_new[]" />
@endfor
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#value
image id should be the key of part_img[]
<input type="file" name="part_img[{{ $image->id }}]" accept=".png, .jpg, .jpeg ,gif,svg" />
imagine have 4 upload input, user upload image on 3 and 4 input, you will get this
null, null, has upload, has upload
@newbie360 I dd($request) but it still null
"part_img" => array:1 [▼
0 => null
]
how can I get this id ?
Does the changed image have the same name, which make looping over harder.
Whereas with different names you can compare a "known" array of existing with new array of images, then just deal with the changed ones. You can use the php in_array.
Similar to https://gist.github.com/jimgwhit/3c1026871ca4246d6ae9f71012ba8e4d
It's not the same but might give you an idea.
Please or to participate in this conversation.