File upload not getting "stored" with record

I have a super basic file upload:
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\FileUpload::make('File Upload')
->columnSpan('full')
->preserveFilenames()
->acceptedFileTypes(['image/png'])
->maxSize(1024)
->storeFileNamesIn('file_name')
->enableDownload(),
Forms\Components\TextArea::make('notes')
->columnSpan('full')
->rows(4)
......
]);

}
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\FileUpload::make('File Upload')
->columnSpan('full')
->preserveFilenames()
->acceptedFileTypes(['image/png'])
->maxSize(1024)
->storeFileNamesIn('file_name')
->enableDownload(),
Forms\Components\TextArea::make('notes')
->columnSpan('full')
->rows(4)
......
]);

}
I can successfully upload an image, see it in the file system (storage folder), get no console or PHP errors etc. but when I save, then refresh the page, the file preview is gone, as if the file was never uploaded, or like I'm looking at a blank/new record. This is probably something really obvious, but what am I missing here? Is there some special database field (other than file_name) that I need to have configured? (Also, my APP_URL is definitely configured correctly). Or do I need the Spatie plugin to preserve image previews?
5 Replies
toeknee
toeknee2y ago
Are you casting it to array in your model as per the docs?
benshawuk
benshawukOP2y ago
Thanks for responding. I thought that was referring to when multiple uploads are allowed? Either way, it still doesn't work. "If you're saving the file URLs using Eloquent"... How else would I be saving them?
benshawuk
benshawukOP2y ago
It's writing to the DB field, but still nothing displays when I refresh the page..
Patrick Boivin
Hi @benshawuk, I'm not sure, this is just a shot in the dark... I've never used storeFileNamesIn(), I've always just used the column name in make() and customized the label:
Forms\Components\FileUpload::make('main_image_upload')
->label('Main image upload')
->columnSpanFull(),
Forms\Components\FileUpload::make('main_image_upload')
->label('Main image upload')
->columnSpanFull(),
The other thing that comes to mind is php artisan storage:link, I often forget on new projects... Did you run that?
benshawuk
benshawukOP2y ago
Ah yeah, that was it! Doh! I knew it would be something simple (like me being an idiot and naming the form component without any relation to the database). Thank you!

Did you find this page helpful?