NameUnknown's avatar

spatie/medialibrary method AddMultipleMediaFromRequest adds only one file

I try to create a mode with several images attached. The code is as follows:

if(request()->hasFile('images'))
            $item->addMultipleMediaFromRequest(['images'])
                ->each(function($fileAdder){
                    $fileAdder->preservingOriginal()->toMediaCollection('item_images);
                });

The Postman form-data looks as follows:

images[]: 3 files

But only the last image from the array is added. I also tried to add images with different keys into the Postman request and used the addAllMediaFromRequest function, but it still uploads only the last image while ignoring all others

0 likes
3 replies
Isaacshort's avatar

You can modify your code:

if (request()->hasFile('images')) { $item->addMultipleMediaFromRequest(['images']) ->each(function ($fileAdder) { $fileAdder->preservingOriginal()->toMediaCollection('item_images'); }); } Also visit: https://www.followmy-health.com/

NameUnknown's avatar

@Isaacshort I did this, too, but it didn't help. Images are OK and work just fine when you add just one image. But if you add them as array, only the last one is added.

NameUnknown's avatar

The reason was simple: I forgot to remove the singleFile trait in the model

Please or to participate in this conversation.