Filament File Upload Custom Relationship

Hello, I've been using Filament to build a Dashboard that has a file upload, I've searched through the documentation and github and could not find an answer so I'm hoping I will find one here. I want to have a file upload that will create a new eloquent model and save it to the database. I have a JobPosting model that it's name, content, it's tied to the user and it should have mutliple files that are tied to it. So for example a Job Posting of the name 'New Job' can have three images and one PDF document uploaded. Migration code:
public function up(): void
{
Schema::create('job_posting_files', function (Blueprint $table) {
$table->id();
$table->string('original_name');
$table->string('stored_name');
$table->foreignId('job_posting_id')->nullable()->constrained()->cascadeOnDelete();
$table->timestamps();
});
}
public function up(): void
{
Schema::create('job_posting_files', function (Blueprint $table) {
$table->id();
$table->string('original_name');
$table->string('stored_name');
$table->foreignId('job_posting_id')->nullable()->constrained()->cascadeOnDelete();
$table->timestamps();
});
}
This model is called JobPostingFile and it has it's original name, the stored name which will be hashed and it's tied to a JobPosting. One Job Posting can have multiple Job Posting Files. Migration code:
public function up(): void
{
Schema::create('job_posting_files', function (Blueprint $table) {
$table->id();
$table->string('original_name');
$table->string('stored_name');
$table->foreignId('job_posting_id')->nullable()->constrained()->cascadeOnDelete();
$table->timestamps();
});
}
public function up(): void
{
Schema::create('job_posting_files', function (Blueprint $table) {
$table->id();
$table->string('original_name');
$table->string('stored_name');
$table->foreignId('job_posting_id')->nullable()->constrained()->cascadeOnDelete();
$table->timestamps();
});
}
How do I make a form in Filament where I can upload multiple files and for each of those files a new JobPostingFile model will be created, it's original name, stored name will be created and it will be linked to the JobPosting model by it's id? Help would be really appreciated, I have spent a month and a half and could not find a solution.
4 Replies
arnaudsf
arnaudsf3w ago
Hello, 3 ways to do that : 1/ Use a relation manager 2/ Use a repeater 3/ On your FileUpload, use multiple() and ->relationship(), as well Filament provide a method to preserve the original name. In Your JobPosting model, create a HasMany relationship As well, just for "good practices", prefer put the foreign keys at the beginning of your migration, after the id
Chaddus Maximus
Chaddus MaximusOP3w ago
Cool, thanks, I'll try to implement it with the relationship manager, I'll read up on that. Thanks for the tip, I'll put the foreign keys at the beginning. I had no idea that was a standard practice.
paulo casimiro
hello, I have a same question, how to you resolved it?
arnaudsf
arnaudsf2w ago
hello paulo, in this case, a repeater with a relationship can help
Want results from more Discord servers?
Add your server