Hussien Elshazly's avatar

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 ?

0 likes
13 replies
jlrdw's avatar

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.

Hussien Elshazly's avatar
 @foreach ($part->images as $key=>$image)
      <input type="file" name="part_img[]" accept=".png, .jpg, .jpeg ,gif,svg" value="{{ $image->id }}" />
@endforeach
jlrdw's avatar

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.

Hussien Elshazly's avatar

@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

Hussien Elshazly's avatar

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
jlrdw's avatar

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.

1 like

Please or to participate in this conversation.