F
Filament14mo ago
Fonz

Custom column to display a pivot of the current user and tenancy relationship

I would like to display a boolean column of the users and the currenty tenant relation in a table and I am a bit lost. Can someone point me to the right direction? Thanks
Solution:
With a bit of help from chatgpt: User.php ```php public function getAcceptedForCurrentTenantAttribute()...
Jump to solution
3 Replies
cheesegrits
cheesegrits14mo ago
Not sure what you mean by " a boolean column of the users and the currenty tenant relation". Can you explain exactly what you are trying to show?
Fonz
FonzOP14mo ago
I am using stancl/tenancy for a plateform with tenancy. Users create an account and ask permission to access a tenant. When they register, I attach the tenant to a user and save some variables to the pivot table. On of those columns is "accepted" with a default value of 0. In filament, the user resource displays all the users that have the relationship of the current tenant. I now would like to display true or false (if they have been accepted or not) in a column based on the value from the pivot. I would like to do something like this $user->tenants()->where('id', $tenantId)->first(); and display the "accepted" column. Currently if I do "tenants.id" all the ids of the attached tenants are displayed. "tenants.accepted" returns nothing, probably because there is not with() in the query. My question is how to I add a custom query to a column?
Solution
Fonz
Fonz14mo ago
With a bit of help from chatgpt: User.php
public function getAcceptedForCurrentTenantAttribute()
{
$userTenant = $this->tenants->firstWhere('id', \Filament\Facades\Filament::getTenant()->id);
return $userTenant ? $userTenant->pivot->accepted : 'N/A';
}
public function getAcceptedForCurrentTenantAttribute()
{
$userTenant = $this->tenants->firstWhere('id', \Filament\Facades\Filament::getTenant()->id);
return $userTenant ? $userTenant->pivot->accepted : 'N/A';
}
UserResource.php
TextColumn::make('Accepted for Current Tenant', 'acceptedForCurrentTenant')
TextColumn::make('Accepted for Current Tenant', 'acceptedForCurrentTenant')
This seems to work
Want results from more Discord servers?
Add your server