Multi-tenancy for models with no direct relation to user

If I understand the multi-tenancy I always need to pass the organization relation, since this is what my tenancy is referring to. Sadly at some models I don't have direct relation to the user or the user is a Many to Many or One to Many relation so that I can get to the organization. I get the following error. I already have set the tenant for the panel by using ->tenant(Organization::class, slugAttribute: 'slug'). I can change the Ownership of the Relationship Name for example protected static ?string $tenantOwnershipRelationshipName = 'users'; as per Resource but since I'm going from the Media -> Game -> User -> Organization this looks very complicated tbh. I've added the following code in Models/Media.php order to display something.

public function organization()
{
return $this->game()->with(
[
'user.organizations' => fn (Builder $builder) => $builder->where('organizations.id', Filament::getTenant()->id)
]
);
}

public function organization()
{
return $this->game()->with(
[
'user.organizations' => fn (Builder $builder) => $builder->where('organizations.id', Filament::getTenant()->id)
]
);
}
If I remove it I get the error from below. The error itself is very self explanatory but I still feel like I'm not doing something right.
The model [App\Models\GameMedia] does not have a relationship named [organization]. You can change the relationship being used by passing it to the [ownershipRelationship] argument of the [tenant()] method in configuration. You can change the relationship being used per-resource by setting it as the [$tenantOwnershipRelationshipName] static property on the [App\Filament\Resources\GameMediaResource] resource class.
The model [App\Models\GameMedia] does not have a relationship named [organization]. You can change the relationship being used by passing it to the [ownershipRelationship] argument of the [tenant()] method in configuration. You can change the relationship being used per-resource by setting it as the [$tenantOwnershipRelationshipName] static property on the [App\Filament\Resources\GameMediaResource] resource class.
If I understand correctly I need to go in every model and define the so called organization relationship and from there fetch the right organization going trough the user?
Solution:
Just link game to organization too :
$table->foreignId('organization_id')->constrained()->cascadeOnDelete();
$table->foreignId('organization_id')->constrained()->cascadeOnDelete();
So like that, you can well tenant everything and if you delete an organization, it will automatically delete games too 🙂...
Jump to solution
5 Replies
Melomancheto
MelomanchetoOP2w ago
Someonе? 🥹
arnaudsf
arnaudsf2w ago
Did you try to create resource for non tenant model ? If yes, I think it's not the good way to do. All the non-tenant, relationship are done with the relation manager. So you just have to give the relationship of your main domain who is linked to the tenancy So GameResource is tenant of Organization and GameMedia is a RelationManager of GameResource
Melomancheto
MelomanchetoOP2w ago
First of all, thank you @arnaudsf for trying to help. SO the idea is that I have a Game Resource, this resource is splitted into 2-3 resources for ease of the user. They are: - Game Application - Game Media They are both related to the Game. The game itself is tied to a user, who is tied to a Organization. I don't know if I'm going the right way, but I consider them as tenant resources, because they need to be managed by the organization
Solution
arnaudsf
arnaudsf2w ago
Just link game to organization too :
$table->foreignId('organization_id')->constrained()->cascadeOnDelete();
$table->foreignId('organization_id')->constrained()->cascadeOnDelete();
So like that, you can well tenant everything and if you delete an organization, it will automatically delete games too 🙂
Melomancheto
MelomanchetoOP2w ago
so If I do that and for example go in the GameMedia Resource and I add protected static ?string $tenantOwnershipRelationshipName = 'game'; it should work, right? It will know that the ownership of the organization is contained by the game I've tested it and it works, but I just wan to be sure that I understand
Want results from more Discord servers?
Add your server