Access Filebag File Array in a Foreach Loop

I've been trying to access this filebag from the request and I've had no luck:

This is what I'm sending:

This is what I recieve when doing dd($request):

yet when trying to output or access it:

for example:

dd($request->file('files')[0]);

it outputs

message: "Cannot use object of type Symfony\Component\HttpFoundation\FileBag as array",

I have tried aswell

dd($request->file('files'));

but it outputs NULL

and I have tried too:

$t = $request->files->all();
dd($t);

this one outputs: []

The way I'm sending it is using axios

let formData = new FormData();
                formData.append('draft', true);
                formData.append('global_id', this.paragraph.ManualBook_SectionParagraphGlobalID);
                formData.append('newparagraph', this.newParagraph.tobeCreated);
                formData.append('paragraph_id', this.paragraph.ManualBook_SectionParagraphGlobalID);
                formData.append('display_order', this.paragraph.DisplayOrder);
                formData.append('section_id', this.current_section);
                formData.append('RCBulletinNumber', this.paragraph.RCBulletinNumber);
                formData.append('paragraph_title', this.paragraph.ParagraphTitle);
                formData.append('paragraph_text', this.paragraph.ParagraphText);
                formData.append('go_live_date', this.getGoodDate(this.edited_paragraph.go_live_date));
                formData.append('revision', this.edited_paragraph.revision);
                formData.append('revision_number', this.paragraph.RevisionVersion);
                formData.append('files', this.files);

                axios.post(window.baseURL + 'api/admin/paragraph_editor', {
                    formData
                }, {
                        headers: {
                        'Content-Type': 'multipart/form-data'
                        }
                    }....

Any idea why is this happening?

1 Answer

If you want to upload multiple files with FormData API via an AJAX request, then you need to append each and every file to a FormData object. Assuming your input file field as the below:

<input type="file" id="files" name="files[]" multiple>

Assuming this.files will be containing multiple files. So we will loop over the files to append them to the FormData object as the below:

let formData = new FormData();

Array.from(this.files).forEach(function(file){
    formData.append('files[]', file);
});

And now you can grab those files in your controller's method as the below:

$request->file('files');

Hope this would help you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Elena Rostova

Elena Rostova

Lead Health, Wellness & Medical Journalist

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.

Share this article
Twitter Facebook Pinterest