Use record ID for file folder path
How would I use the record ID in the folder path to store files?
I have tried the following
but get an error when creating new because the record does not exist yet. It does work if I create the record first then edit as the record already exists.
Solution:Jump to solution
I got this working with the following
```
protected function mutateFormDataBeforeCreate(array $data): array
{...
6 Replies
The source of the ID would be controlled presumably by the database and not the application, that would be why. Is this just to cleanly separate files? Could you not let it save where it’s saving now and programmatically move it afterwards?
Hi @SirFat , yeah it was just to make them clean and unique while maintaining the filename. I guess they could be moved with an eventListner but then would have to write that for a new and edit event.
Yeah because of when the ID assignment occurs, if separation is that important - you don’t really have a choice 🙂
I think another way would be to create a separate model for files and then in the form it creates the record first then the file.
I've just found the docs about lifecycle hooks too.
Solution
I got this working with the following
Only works on the form page not in a modal. Before the data is saved it gets the next available id from the model and then uses that.
In the form I have this
->directory(fn ($record) => $record ? 'project-shapes/' . $record->id : 'project-shapes')
So if you're in the edit screen it works as normalgreat work 🙂