FilamentF
Filament14mo ago
knous

Displaying data from BelongsToMany relationship in UserRelationManager

I'm having trouble displaying the Role title in a UserRelationManager table column. Here’s the setup I currently have: Model: Account.php

public function users()
{
return $this->belongsToMany(User::class, 'user_accounts', 'account_d', 'user_id', 'id', 'id')
->withPivot('id', 'role_id', 'email')
->withTimestamps();
} Model: User.php

public function accounts()
{
return $this->belongsToMany(Account::class, 'user_accounts', 'user_uuid', 'account_uuid', 'uuid', 'uuid')
->withPivot('id', 'role_id', 'email')
->withTimestamps();
} Model: UserAccount.php (Pivot Model)

public function user()
{
return $this->belongsTo(User::class, 'user_id', 'id');
}

public function account()
{
return $this->belongsTo(Account::class, 'account_id', 'id');
}

public function role()
{
return $this->belongsTo(Role::class, 'role_id', 'id');
} Displaying Columns in UserRelationManager

I’m trying to display both role_id and role.title in the table columns like this:

Tables\Columns\TextColumn::make('role_id')
->label('Role ID')
->sortable(),

Tables\Columns\TextColumn::make('role.title')
->label('Role Title')
->searchable()
->sortable(),

However, only the role_id displays correctly. role.title doesn’t show up as expected. Does anyone know why role.title isn’t displaying in the column, or how I might fix this? Any help would be appreciated!

Thanks!
Was this page helpful?