How do I persist FileUpload's result?
I realize this is a simple question but I cannot find the answer.
I've got a resource's Form with a FileUpload input. I've read the documentation but I can't for the life of me find any information about where/how to persist information about the upload to the database.
I've already got a File model (which is the model that the resource's form is connected to) but don't know where to put any code in FileResource to handle the upload.
Thank you!
7 Replies
It’s handled automatically unless you need to override how it it saved.
Is there documentation for the fields it saves and what column names it expects?
I guess I understand that the fields created in the form correspond to columns in the model. But there are attributes for the file that are derived from the actual upload - original filename, size, path. How can they be persisted?
(Follow up question which I realize is more ocmplicated... if I have a generic File table that stores any uploaded file and a different Resource table that stores every resource, only some of which are uploaded files, how do I set up the form in Resource to have a FileUpload input that creates a File and then stores the file_id in Resource?)
If you need all of that information you would need to have a separate table and model for media and use a relationship. Or you could use Filaments spatie media library plugin, or something like #media-library-pro or #curator
I thought I did have a separate table for the media - it's File. But it sounds like what you're telling me is that there's no mechanism for custom behavior in the resource form saving/upload process?
There's no exposed place in the Filmament lifecycle where I could write custom behavior while still having the $_FILES object (totally great if there's a Filament wrapper around it) still accessible?
I'll admit I haven't played around with the upload-related plugins yet. I'll spend some time with those today.
->saveUploadedFileUsing()
He’s an example using a custom FileUpload component https://github.com/awcodes/filament-curator/blob/3.x/src/Components/Forms/Uploader.php
GitHub
filament-curator/src/Components/Forms/Uploader.php at 3.x · awcodes...
A media picker plugin for Filament Panels. Contribute to awcodes/filament-curator development by creating an account on GitHub.
Hmmm... interesting!! Thank you!