Tenant with an extra layer of ownership - uploading documents

Hi. I have set up Filament with multitenancy, and I am using a classic "Team" model. I have one more layer below a team, that I call buckets: A team can have many buckets, and a bucket can belong to one team. Then, under a bucket, the user can upload many documents, meaning that: a bucket can have many documents, and a document can belong to one bucket. Now, I have set up a new resource called DocumentResource, but I am simply unable to figure out how I can upload a simple document via a header action, since the document should belong to a bucket, and not to a team directly. In my App panel, I have specified this:
tenant(Team::class, ownershipRelationship: 'bucket')
tenant(Team::class, ownershipRelationship: 'bucket')
Then, on the DocumentResource I have set a relationship:
protected static ?string $model = Document::class;
protected static ?string $tenantRelationshipName = 'buckets';
protected static ?string $model = Document::class;
protected static ?string $tenantRelationshipName = 'buckets';
Then, under the ListDocuments page, I would like to have the "Upload Document" modal:
protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make()
->form([
FileUpload::make('name')
->required()
->storeFileNamesIn('original_filename')
])
];
}
protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make()
->form([
FileUpload::make('name')
->required()
->storeFileNamesIn('original_filename')
])
];
}
When I upload, it fails, since Filament is looking for a team_id on the documents table. But there is none, only a bucket_id
5 Replies
Sjoerd24
Sjoerd2411mo ago
Just put this in your documentresource that shouldn’t scope for tenants: protected static bool $isScopedToTenant = false; Although you have to be careful to limit access to the buckets yourself.
oliverbusk
oliverbuskOP11mo ago
But how can I then upload a document and relate it to a bucket? After some more thinking -- basically all the users will see is scoped like this: Team -> Bucket -> Data Is there a better way to achieve this, so the data always relates to the active bucket?
Sjoerd24
Sjoerd2411mo ago
Why not give the bucket a team_id then?
oliverbusk
oliverbuskOP11mo ago
The bucket does indeed have a team_id, perhaps I am just confused about how to scope this correctly..
david1686624
david168662411mo ago
I got the same problem. The way around it is that, I add team_id in model Document's $fillable. In ListDocument Actions\CreateAction::make() ->form([ FileUpload::make('name') ->required() ->storeFileNamesIn('original_filename') ]) ->mutateFormDataUsing(function (array $data): array { $data['team_id'] = Filament::getTenant()->id; return $data; }); and you should remove $tenantRelationshipName from youor DocumentResource.
Want results from more Discord servers?
Add your server