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.
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.
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:Jump to solution
Just link game to organization too :
So like that, you can well tenant everything and if you delete an organization, it will automatically delete games too 🙂...
5 Replies
Someonе? 🥹
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
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
Just link game to organization too :
So like that, you can well tenant everything and if you delete an organization, it will automatically delete games too 🙂
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