How to Set a Default Image in Laravel Filament FileUpload?

How can I set a default banner image in the file uploader?
I'm using Laravel's Filament FileUpload component, and I want to display a default banner image when the page loads. If the user uploads a new image, it should replace the default. However, if no image is uploaded, the default banner image should remain.
I've tried using the default() method and the formatStateUsing() function, but neither seems to work:
Forms\Components\FileUpload::make('banner')
->disk('banner')
->directory('banner')
->image()
->imageEditor()
->maxSize(3072) // 3MB limit
->acceptedFileTypes(['image/jpeg', 'image/png', 'image/webp'])
->formatStateUsing(fn($state) => $state ?? ['banner/banner.png']) // Default banner image
->validationMessages([
'banner.dimensions' => __('validationBannerDimensions'),
'banner.max' => __('validationBannerMax'),
])
->rules([
'dimensions:min_width=500,min_height=500,max_width=1000,max_height=1000'
])
->helperText(__('validationBannerHelperMsg')),
Forms\Components\FileUpload::make('banner')
->disk('banner')
->directory('banner')
->image()
->imageEditor()
->maxSize(3072) // 3MB limit
->acceptedFileTypes(['image/jpeg', 'image/png', 'image/webp'])
->formatStateUsing(fn($state) => $state ?? ['banner/banner.png']) // Default banner image
->validationMessages([
'banner.dimensions' => __('validationBannerDimensions'),
'banner.max' => __('validationBannerMax'),
])
->rules([
'dimensions:min_width=500,min_height=500,max_width=1000,max_height=1000'
])
->helperText(__('validationBannerHelperMsg')),
How can I correctly set the default image so that it appears when the form loads?
1 Reply
Nikunj Gadhiya
Nikunj GadhiyaOP3w ago
Stack Overflow
How to Set a Default Image in Laravel Filament FileUpload?
How can I set a default banner image in the file uploader? I'm using Laravel's Filament FileUpload component, and I want to display a default banner image when the page loads. If the user uploads a...

Did you find this page helpful?