Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

earmsby's avatar

are relations on a model accessible in a RelationManager

I have a model: Accounts with this relation set up:

public function roles(): BelongsToMany
    {
        return $this->belongsToMany(Role::class);
    }

In the view for this Filament resource I have this:

class AccountInfolist
{
    public static function configure(Schema $schema): Schema
    {
        return $schema
            ->components([
... some other fields ...
                        TextEntry::make('roles.name')
                            ->label('Account Roles')
                            ->columnSpanFull()
                            ->badge(),

This works perfectly when I access this view directly from the Accounts resource.

Then I have another model: Contract which has a relation to Account set up like so:

public function accounts(): BelongsToMany
    {
        return $this->belongsToMany(Account::class);
    }

In the Contract filament resource I have a relation manager AccountsRelationManager. I can add new accounts and link existing ones without any issue. However, when I access the view for the account from the relationManager with the same TextEntry component as on the Account resource view, the roles are blank. So, it appears that account->roles() is not valid in this context.

Is there a way to display the related roles for the Account on the view in the relationManager? (I hope I'm asking this question correctly.)

0 likes
4 replies
LaryAI's avatar
Level 58

Certainly! This is a common situation when working with Filament's RelationManagers.

Problem Recap: When you use a relation manager (e.g. AccountsRelationManager from your Contract resource), Eloquent may not eager load the nested relations (in this case, roles of each account), which means $account->roles will be empty unless explicitly loaded.

Why? In the main Accounts resource, Filament's table/form typically eager loads all columns/relations you use. But in a RelationManager, only the direct relation (accounts) is loaded—nested relations like roles are not eager-loaded by default.

Solution: Override the getTableQuery() or getEloquentQuery() method in your AccountsRelationManager and eager-load the roles relation.

Example:

class AccountsRelationManager extends RelationManager
{
    protected static string $relationship = 'accounts';

    public static function getTableQuery(RelationManager $livewire)
    {
        return parent::getTableQuery($livewire)
            ->with('roles'); // Eager load roles
    }

    // If you are using a different Filament version or Table, try getEloquentQuery:
    // public static function getEloquentQuery(): Builder
    // {
    //     return parent::getEloquentQuery()->with('roles');
    // }
}

Now, roles will be available when you access them in your view (e.g. roles.name in your component).

Note:

  • Make sure you use .with('roles'), not .with('roles.name').
  • If you use getTableQuery(), be sure it returns a query builder instance. If your version uses getEloquentQuery(), override that instead.

Summary:
Yes, relations are accessible in a RelationManager as long as you eager load them in the respective query. Just add a .with('roles') to your query override.

Let me know if you need an example specifically for forms or InfoList instead of a Table!

earmsby's avatar

Hmmm... I tried that but my IDE says "Method getEloquentQuery() not found in \Filament\Resources\RelationManagers\RelationManager" and in fact adding that to my AccountsRelationManager does not solve the issue.

FWIW I am using Filament 4.x

earmsby's avatar

Next I tried

return $table
            ->modifyQueryUsing(function (Builder $query) {
                $query->with('roles');
            })
[ ... ]

That didn't work either. Stumped.

earmsby's avatar

Ok, when I added modifyQueryUsing to the return $table and also added a column for role, that works. I have this on the table:

return $table
            ->modifyQueryUsing(function (Builder $query) {
                $query->with('roles');
            })
            ->recordTitleAttribute('account_number')
            ->columns([
                TextColumn::make('account_number')
                    ->label('Account ID')
                    ->searchable(),
                TextColumn::make('display_name')
                    ->label('Account Name')
                    ->searchable(),
                TextColumn::make('roles.name')
                    ->label('Account Roles')
                    ->default('[roles should appear here]')
                    ->badge(),
            ])
[...]

The issue is that the view doesn't seem to have access to the relation and it doesn't seem possible to do a similar modifyQueryUsing on the view action.

This is what I have on the infolist:

    public function infolist(Schema $schema): Schema
    {
        //return AccountInfolist::configure($schema);
        return $schema
            ->components([
                Section::make('Account Info')
                    ->columnSpanFull()
                    ->columns(3)
                    ->icon(Heroicon::InformationCircle)
                    ->schema([
                        TextEntry::make('account_number')
                            ->columnSpan(2),
                        TextEntry::make('account_type'),
                        TextEntry::make('roles.name')
                            ->label('Account Roles')
                            ->columnSpanFull()
                            ->badge(),
						[ ... other fields ...]
                    ]),
            ]);
    }

The roles show on the edit action and on the table but not the infolist. Surely there is a way to do this?

Please or to participate in this conversation.