Use resource record ID in file upload path
I need to use the record id in my file path when uploading files. Is this possible, and if so, how would I go about doing that?
This is my form:
I would like to use something like, ->
directory('claim-attachments' . $this->id)
5 Replies
Not sure if thats possible, since when you are uploading and you are creating a record, and thus since its not yet created it will be null
There is a
mutateFormDataBeforeCreate()
method you can call in the create page, but the attachments seem to be empty in the posted array, since they are uploaded in realtime. So I could get it to work on the edit page, but not during create.
A workaround could potentially be to hide the file attachments during creation and only show it on the edit page. Is something like that possible?
As amazing as filament is, I feel there are a lot of hacking needed in order to get basic things done unfortunately ;/
So, here is my solution using the mutateFormDataBeforeCreate()
function when the form is submitted. It is based off of another user's code. It's not pretty, but it does work:
I am not yet going to mark this post as solved, since I would like to know if there isn't another easier way to do this. Thanks.Cant you just do this?
->directory(Claim::max('id') + 1)
I think you are overcomplicating itThat's a great idea, thanks. Let me see if I can use that way. Thank you!
Wow, thank you Matthew. I was over complicating it, your solution does indeed work.
I was in a similar position, but went another way. I wanted all files in all resources to be in their respective directories. Files uploaded to model Product, attribute product_gallery for example, needed to be uploaded to
storage/app/private/product/{product_id}/product_gallery
I created a ModelObserver that obeserves all models.
In both the created()
and updated()
method, I call the same logic, which:
1. gets all model attributes, filters only fields which contain paths or an array of paths
2. check if the files have already been moved, if not move them rename($oldFilePath, $newFilePath);
3. update the attribute in the database with the new path
Right now, this works as it should, only problem is that filament doesn't refresh the values after updating (after page refresh all is fine).
To overcome this issue, I removed the ModelObserver::update()
logic, and let filament handle this:
I don't like having similar logic in two places, but in this case, I don't see any other solution