0

I’m working on a Laravel API that allows users to upload files as part of a nested form-data request. However, the files are not being detected properly in the request. Below is my current setup:

Request Structure:

The request is submitted as form-data using Postman. Here’s the structure:

  • skill_type (string): required
  • skill_description (string): required
  • portfolios (array): optional

Each portfolio includes:

  • description (string): optional

  • files (array of file uploads): optional

Laravel Form Request Validation Rules:

public function rules(): array
{
    return [
        'skill_type' => 'required|string|max:255',
        'skill_description' => 'required|string|max:2000',
        'portfolios' => 'nullable|array',
        'portfolios.*.files' => 'nullable|array',
        'portfolios.*.files.*' => 'file|mimes:jpg,jpeg,png,pdf|max:2048',
    ];
}

Controller Logic:

public function register(StoreWorkerRequest $request): JsonResponse
{
    $validatedData = $request->validated();

    return response()->json([
        'all_inputs' => $request->all(),
        'file_inputs' => $request->file('portfolios'),
    ]);
}

Files are attached as form-data in Postman, with the field names set as:

  1. portfolios[0][files][] for the first file in the first portfolio, and
  2. portfolios[1][files][] for the first file in the second portfolio.

Problem:

Despite attaching files correctly in Postman, the file_inputs response in Laravel shows empty arrays for files. The response looks like this:

{
    "all_inputs": {
        "skill_type": "Compete",
        "skill_description": "Insane designer",
        "portfolios": [
            {
                "description": "Portfolio 1 description",
                "files": [{}]
            },
            {
                "description": "Portfolio 2 description",
                "files": [{}]
            }
        ]
    },
    "file_inputs": [
        {
            "files": [{}]
        },
        {
            "files": [{}]
        }
    ]
}

What I’ve Tried:

  1. Ensured the Content-Type header is not manually set in Postman (Postman automatically sets it to multipart/form-data).
  2. Verified that files are being correctly attached in the form-data section of Postman.
  3. Used $request->all() and $request->file() to inspect the inputs in Laravel.
  4. As suggested by @C3roe I also dumped the $_FILE and I got the array of files selected in the postman as:
"file": {
       "portfolios": {
           "name": [
               {
                   "files": [
                       "c-sm.jpg"
                   ]
               },
               {
                   "files": [
                       "c-sm.jpg"
                   ]
               }
           ],
           "full_path": [
               {
                   "files": [
                       "c-sm.jpg"
                   ]
               },
               {
                   "files": [
                       "c-sm.jpg"
                   ]
               }
           ],
           "type": [
               {
                   "files": [
                       "image/jpeg"
                   ]
               },
               {
                   "files": [
                       "image/jpeg"
                   ]
               }
           ],
           "tmp_name": [
               {
                   "files": [
                       "C:\\Users\\...\\AppData\\Local\\Temp\\phpE252.tmp"
                   ]
               },
               {
                   "files": [
                       "C:\\Users\\...\\AppData\\Local\\Temp\\phpE253.tmp"
                   ]
               }
           ],
           "error": [
               {
                   "files": [
                       0
                   ]
               },
               {
                   "files": [
                       0
                   ]
               }
           ],
           "size": [
               {
                   "files": [
                       2701
                   ]
               },
               {
                   "files": [
                       2701
                   ]
               }
           ]
       }
   },

VALIDATION FOR THE FILES

'portfolios' => 'nullable|array',
 'portfolios.*.files' => 'nullable|array',
 'portfolios.*.files.*' => 'file|mimes:jpg,jpeg,png,pdf|max:2048',

Expected Behavior: The file_inputs array should contain the uploaded file details (e.g., original_name, mime_type, size, etc.) for each attached file.

Question: Why are the files in my nested form-data request not being detected by Laravel, and how can I fix this to properly handle file uploads with a nested array structure?

9
  • What do you get when you dump $_FILES? Commented Dec 9, 2024 at 8:37
  • I got the array of uploaded files when I dumped $_FILES. I will edit my question and add the result now. Commented Dec 9, 2024 at 9:51
  • 1
    If $_FILES shows the uploaded files as expected (with a size, and no error), then most likely it is your validation setup, that makes them not available via the Laravel method. max:2048 - that's just 2KB, sure your files are smaller than that? Commented Dec 9, 2024 at 9:55
  • Can you inspect my validation. If the file size is less than 2048, shouldn't the validation throw a 422 ? I added the file validation rule to my OP Commented Dec 9, 2024 at 10:02
  • 1
    Your $_FILES dump says the size of both files was 2701, which is clearly not less than 2048. Commented Dec 9, 2024 at 10:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.