“Undefined array key 0” when uploading an image

Solution It was a issue with my local php environment. After using a newer PHP version or testing it on my live server it works. If you get this issue, try install Laravel Herd or make a clean php install. What I’m trying to do and why I’m building an admin interface with Laravel 12, Filament 3.3 to manage my menu categories. Each category should optionally have a cover image:
FileUpload::make('cover_image')
->label('Cover Image')
->image()
->directory('category-covers')
->multiple(false)
->maxFiles(1)
->nullable();
FileUpload::make('cover_image')
->label('Cover Image')
->image()
->directory('category-covers')
->multiple(false)
->maxFiles(1)
->nullable();
What I did so far - Added the cover_image column via migration and included it in my Category model’s $fillable. - Ran php artisan view:clear, config:clear, etc. - Enforced single-file upload with multiple(false) and maxFiles(1). - created a single resource just for file uploads to test it Still, the upload process fails with:
ErrorException: Undefined array key 0
at vendor/livewire/livewire/src/Features/SupportFileUploads/WithFileUploads.php:40
ErrorException: Undefined array key 0
at vendor/livewire/livewire/src/Features/SupportFileUploads/WithFileUploads.php:40
Relevant parts of my setup
// composer.json (excerpt)
"require": {
"php": "^8.2",
"filament/filament": "^3.3",
"filament/spatie-laravel-translatable-plugin": "^3.3",
"laravel/framework": "^12.0",
"laravel/tinker": "^2.10.1",
"spatie/laravel-translatable": "^6.11"
},
// composer.json (excerpt)
"require": {
"php": "^8.2",
"filament/filament": "^3.3",
"filament/spatie-laravel-translatable-plugin": "^3.3",
"laravel/framework": "^12.0",
"laravel/tinker": "^2.10.1",
"spatie/laravel-translatable": "^6.11"
},
// app/Models/Category.php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;

class Category extends Model
{
use HasTranslations;

public $translatable = ['name'];
protected $fillable = [
'parent_id',
'name',
'order',
'cover_image',
'is_food',
];
}
// app/Models/Category.php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;

class Category extends Model
{
use HasTranslations;

public $translatable = ['name'];
protected $fillable = [
'parent_id',
'name',
'order',
'cover_image',
'is_food',
];
}
// In CategoryResource form()
FileUpload::make('cover_image')
->label('Cover Image')
->image()
->directory('category-covers')
->multiple(false)
->maxFiles(1)
->nullable(),
// In CategoryResource form()
FileUpload::make('cover_image')
->label('Cover Image')
->image()
->directory('category-covers')
->multiple(false)
->maxFiles(1)
->nullable(),
Question: How can I configure the FileUpload so that Filament/Livewire handles cover_image without throwing “Undefined array key 0”? Any guidance or tips are greatly appreciated!
Solution:
Soo, that's interesting... It works now. I believe it was a issue with my php version. I installed 8.4 and it works, I also installed Herd and it seems like a good application... I will work with it...
Jump to solution
24 Replies
awcodes
awcodes2w ago
You don’t need both multiple and max files. Use one or the other.
Dennis Koch
Dennis Koch2w ago
I think the cover_image still needs to be cast to an array even if it's a single one
OnyxException
OnyxExceptionOP4d ago
Both do not work
Dennis Koch
Dennis Koch4d ago
Can you check the network requests? Sounds like some issue directly with Livewire. It seems it can't find the temp file.
OnyxException
OnyxExceptionOP3d ago
No description
No description
No description
Dennis Koch
Dennis Koch3d ago
I mean network requests via your Dev Tools.
OnyxException
OnyxExceptionOP3d ago
No description
No description
Dennis Koch
Dennis Koch3d ago
What's the response of file-upload?
OnyxException
OnyxExceptionOP3d ago
{"paths":[]}
Dennis Koch
Dennis Koch3d ago
It should look something like this:
paths: [
"/nIh3jyKAighXBqG59huYE3kJrv4CBf-metaTGlua2VkSW4tSGF1cHRiaWxkICgxKS5wbmc=-.png"
]
paths: [
"/nIh3jyKAighXBqG59huYE3kJrv4CBf-metaTGlua2VkSW4tSGF1cHRiaWxkICgxKS5wbmc=-.png"
]
So for some reason your file uploads fail. What dev environment do you use? Is this on Windows?
OnyxException
OnyxExceptionOP3d ago
Yes
Dennis Koch
Dennis Koch3d ago
Laragon?
OnyxException
OnyxExceptionOP3d ago
Nope, just normal dev env in PHPStorm
Dennis Koch
Dennis Koch3d ago
What is "normal dev env"?
OnyxException
OnyxExceptionOP3d ago
I created just a normal project within PHPStorm, run composer commands if needed, or artisan commands. MySQL Workbench and for php I use scoop
Dennis Koch
Dennis Koch3d ago
Do you have a upload_tmp_dir set in your php.ini?
OnyxException
OnyxExceptionOP3d ago
No
Dennis Koch
Dennis Koch3d ago
I guess that's the issue. Set a dir where the tmp files should be stored.
OnyxException
OnyxExceptionOP3d ago
So I set
upload_tmp_dir = "C:\Users\user\temp-uploads\
upload_tmp_dir = "C:\Users\user\temp-uploads\
and it still dont work
Dennis Koch
Dennis Koch3d ago
Can you check whether the setting is actually applied via ini_get('upload_tmp_dir')
OnyxException
OnyxExceptionOP3d ago
Yes, I already checked it. It is
Dennis Koch
Dennis Koch3d ago
Hm, I'm out of ideas. I'd verify whether you can upload a file via a simple form with pure PHP backend. If not it's probably related to your dev env. Have you tried Herd for Windows? Maybe it works better.
Solution
OnyxException
OnyxException3d ago
Soo, that's interesting... It works now. I believe it was a issue with my php version. I installed 8.4 and it works, I also installed Herd and it seems like a good application... I will work with it
Dennis Koch
Dennis Koch3d ago
Great!

Did you find this page helpful?