Users Resource on Multitenant Setup with Pivot Data
So I have a User resource on a panel with multitenancy but I want to only show users who are part of the same tenant (Team model).
My relationship for the tenant looks like this:
Adding
protected static ?string $tenantOwnershipRelationshipName = 'teams';
seems to have done that part just fine. But my pivot table, using a Member model, stores a third relation to a Role model.
How can I get the role name to display as a column on the Users table?
I was playing around with using a custom text column with a state function to get the value like so:
$user->teams->first()->pivot->role->name
This kinda works, except for instead of just grabbing the first team for the user I need a good way to grab the current team. I feel like there should be an easy way to do this but I am not sure with such a complex relationship.8 Replies
Is there a way maybe to setup a custom relationship from the user model direct to the instance of the member model that matches the current tenant maybe so I could just do something like
$user->member->role->name
maybe?You're talking about Roles in the singular, as though users have only one Role. Does that mean in your case "Role" is actually more like a "Member Type"? And not the traditional situation where users can have "many roles"?
In Filament, if there's a Laravel relationship set up between two models, the column-name in a Filament table would use dot-notation, such as
relation_name.field_name
, and could use several levels of dot-notation to traverse through the relationships set up from model to model.
But most table columns display a single value, so unless your field data is a single value, you'll need to either make it pick the first one, or use a column-type that allows displaying of multiple values in one cell.I have the User model and a Team model for multitenancy. I have a Member model which in the pivot table between Users and Teams, but also contains a singular role_id for the member.
All I need to figure out is how to get the Member entry for the current tenant (Team)
It would be much easier to see your application's code, including the models and migrations.
Please post the code that shows your table schemas for User, Team, Member, Role tables as well as all the relationships defined in each of those same Models' model classes. And your UserResource class
Forget the roles, thats the easy part. Members is the pivot between Users and Teams which has many to many relationship. I need to way to get to the member record that matches the current tenant and current user from the current user.
For example I can get to the first member record for a user using
$user->teams->first()->pivot
I just need to get the current tenant instead of which ever tenant is in the first matching member record.Without seeing your table schemas and your model relationships and your Resource code, it's just a bunch of guesswork trying to construct it from a bunch of paragraphs of text, and even then could be wrong without seeing actual code.
Its not complicated. Its a simple many to many relationsip
Do you not know what that looks like?
You seem to be contradicting yourself. Earlier you said "I feel like there should be an easy way to do this but I am not sure with such a complex relationship."
Perhaps an additional scoped relationship added to the model, which pulls only a single record, based on the specified member?