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:
- portfolios[0][files][] for the first file in the first portfolio, and
- 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:
- Ensured the Content-Type header is not manually set in Postman (Postman automatically sets it to multipart/form-data).
- Verified that files are being correctly attached in the form-data section of Postman.
- Used $request->all() and $request->file() to inspect the inputs in Laravel.
- 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?
$_FILES?max:2048- that's just 2KB, sure your files are smaller than that?2701, which is clearly not less than2048.