Nikunj Gadhiya
Nikunj Gadhiya
FFilament
Created by Nikunj Gadhiya on 2/12/2025 in #❓┊help
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?
3 replies