Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to handle multiple file inputs with addMediaFromRequest? #227

Closed
kickthemooon opened this issue May 12, 2016 · 7 comments
Closed

How to handle multiple file inputs with addMediaFromRequest? #227

kickthemooon opened this issue May 12, 2016 · 7 comments

Comments

@kickthemooon
Copy link

So the addMediaFromRequest expects the request key
in my case the request key is images[] so it is an array of files
after which the key becomes and integer.

multiple-file-upload

so $request->file('images') returns this:

array:2 [▼
    0 => UploadedFile {#364 ▶}
    1 => UploadedFile {#356 ▶}
  ]

when i do this

$report->addMediaFromRequest('images')->toCollection('medical-reports');

i get this error

FileCannotBeAdded in FileCannotBeAdded.php line 13:
Only strings, FileObjects and UploadedFileObjects can be imported

and when I do this

foreach($request->file('images') as $image){
            $report->addMediaFromRequest($image)->toCollection('medical-reports');
        }

I get this error

FileCannotBeAdded in FileCannotBeAdded.php line 49:
The current request does not have a file in a key named `C:\wamp64\tmp\php3894.tmp`

Which is the true, the request does not have any key names by the tmp upload path of the file

Now I am not sure how to feed the addMediaFromRequest method...

@freekmurze
Copy link
Member

freekmurze commented May 21, 2016

When uploading multiple files with the same name in the request, do not use addMediaFromRequest

Get the files of the request and add them manually.

Try this:

foreach($request->file('images') as $image) {
   $report->addMedia($image)->toCollection('medical-reports');
}

Reopen if you are still having problems.

@theinhlamaw
Copy link

I was looking for the same answer for Laravel Medialibrary v.7 here. The following code works for me.

foreach($request->file('images') as $image) {
   $report->addMedia($image)->toMediaCollection('medical-reports');
}

I only need to change toCollection() to toMediaCollection(). Thanks @kickthemooon and @freekmurze.

@ridislam
Copy link

@freekmurze how can I show these media in list view page?

@Mohammed-Alama
Copy link

@freekmurze how can I show these media in list view page?

try to add it to specific media collection then use getMedia('collection_name') to get media

@Alhiane
Copy link

Alhiane commented Sep 25, 2021

@theinhlamaw it's slow the server whene using foreach

@tayyabzino19
Copy link

tayyabzino19 commented Jan 31, 2023

You can also try this

 if ($request->hasFile('photos')) {
                $fileAdders = $product->addMultipleMediaFromRequest(['photos'])
            ->each(function ($fileAdder) {
                $fileAdder->toMediaCollection('products');
            }); }

@kareemOyoun
Copy link

kareemOyoun commented Apr 6, 2023

good way to upload multiple media this response returned
"library": [
{
"id": 26,
"name": "How-To-Use-Vue-3-With-Laravel-5-–-2.jpg",
"url": "http://saas.localhost:8000/media/26/How-To-Use-Vue-3-With-Laravel-5-–-2.jpg"
},
{
"id": 27,
"name": "escape_from_tarkov-main-1920x1080.png",
"url": "http://saas.localhost:8000/media/27/escape_from_tarkov-main-1920x1080.png"
}
],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants