How can the FileUpload field in Laravel Filament directory be changed dynamically?
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
How Can I Change the FileUpload Field in Laravel Filament Dynamically? Mastering Dynamic Path Generation
As senior developers working with frameworks like Laravel and Filament, we constantly face scenarios where static configurations fall short. One of the most common and practical challenges is managing file storage paths that depend on the context of the data being edited—in this case, dynamically setting the upload directory based on a related record.
You are looking to implement a solution where a user uploading a file automatically places it into a subdirectory specific to their associated client details (e.g., public/uploads/documents/userfolder). This requires bridging the gap between an Eloquent relationship and the form builder logic in Filament.
This post will walk you through the developer-focused method to achieve dynamic path generation for FileUpload fields within your Laravel Filament application, ensuring clean, scalable, and secure file management.
The Challenge: Dynamic File Path Generation
The goal is to make the storage location of a file dependent on the data in the parent model (e.g., the Client record). A standard FileUpload configuration is usually static. To achieve dynamic behavior, we need access to the related model's attributes at the time the form is rendered.
The suggested syntax you encountered:<code class="language-php">FileUpload::make('name')->disk('public')->directory('documents/'.$foldername),</code>
highlights the intent, but achieving this dynamically requires fetching the $foldername from the related parent record before defining the field.
The Solution: Leveraging Eloquent Relationships in Filament Forms
The key to solving this lies in utilizing Laravel's Eloquent relationships directly within your Filament Resource class. Instead of hardcoding the path, you must fetch the dynamic value from the relationship context provided by the form builder.
Step 1: Define the Relationship Access
Ensure your models are correctly set up with a proper one-to-one or one-to-many relationship. For this example, let's assume your Client model has a relationship to a UserFolder or directly to another parent entity that holds the folder name.
Step 2: Dynamic Path Construction in the Resource
Within the form() method of your Filament Resource, you can access the current record being edited. This allows you to pull the necessary data dynamically.
Here is a conceptual example demonstrating how you would conditionally set the file upload path based on a relationship:
use Filament\Forms\Components\FileUpload;
use Illuminate\Support\Facades\Storage;
// Inside your Resource class's form() method...
FileUpload::make('document_file')
->label('Upload Document')
->disk('public')
->directory(fn (string $record) => 'documents/' . $record->client->folder_name ?? 'default') // Dynamically fetch folder_name
->preserveFilenames()
->multiple(),
Explanation of the Code:
FileUpload::make('document_file'): Defines the file input field.disk('public'): Specifies that we are using the public disk, which is standard for web-accessible files. This aligns perfectly with best practices outlined by resources like Laravel Company.directory(fn (string $record) => 'documents/' . $record->client->folder_name ?? 'default'): This is the core dynamic part. We use a Closure callback function provided by Filament. This closure executes when defining the field, allowing us to reference the current model instance ($record) and access its related data ($record->client->folder_name). The null coalescing operator (?? 'default') acts as a crucial fallback for safety.
Best Practices for File Uploads
When dealing with file uploads in any Laravel application, especially those managed by Filament, security and structure are paramount:
- Disk Management: Always define your storage using appropriate disks (like
public,s3, or local) to manage where the files physically reside. As noted on Laravel Company, understanding disk configuration is fundamental for robust application architecture. - Permissions: Ensure that the web server process (e.g., Apache/Nginx user) has write permissions to the target directory (
public/uploads/...). Misconfigured permissions are a common source of upload failures. - Validation: Always implement strict validation rules on file uploads to ensure only permitted file types and sizes are accepted before saving.
Conclusion
Dynamically changing file upload directories in Laravel Filament is not achieved through a single static configuration, but rather by leveraging the dynamic capabilities of the form builder itself. By utilizing closure functions within field definitions, you can seamlessly connect your data models (Eloquent relationships) directly to the UI configuration. This approach results in highly flexible forms that adapt perfectly to the underlying business logic, making your Filament applications significantly more powerful and developer-friendly.