F
Filament12mo ago
James

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:
public function teams() {
return $this->belongsToMany(Team::class, 'members')
->withPivot('role_id');
}
public function teams() {
return $this->belongsToMany(Team::class, 'members')
->withPivot('role_id');
}
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
James
JamesOP12mo ago
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?
DrByte
DrByte12mo ago
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.
James
JamesOP12mo ago
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)
DrByte
DrByte12mo ago
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
James
JamesOP12mo ago
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.
DrByte
DrByte12mo ago
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.
James
JamesOP12mo ago
Its not complicated. Its a simple many to many relationsip Do you not know what that looks like?
DrByte
DrByte12mo ago
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?
Want results from more Discord servers?
Add your server