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

Forms\Components\FileUpload::make('shape_file')
    ->preserveFilenames()
    // directory is record id of the project
    ->directory(fn ($record) => 'project-shapes/' . $record->id)
    ->previewable(false)
    ->downloadable()
    ->disk('private')
    ->visibility('public'),

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
I got this working with the following

    protected function mutateFormDataBeforeCreate(array $data): array
    {
        // Get the next auto increment from the datahase.
        $nextAutoIncrement = ProjectResource::getModel()::max('id') + 1;
        // Move the file from the temporary storage to the private storage.
        Storage::disk('private')->move('project-shapes/' . $data['shape_file'], 'project-shapes/' . $nextAutoIncrement . '/' . $data['shape_file']);
        // Change the file path to be the next auto increment.
        $data['shape_file'] = 'project-shapes/' . $nextAutoIncrement . '/' . $data['shape_file'];
        
        return $data;
    }

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 normal
Was this page helpful?