© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•15mo ago•
1 reply
urbycoz

Filament table builder with union query

I have a Filament table that displays records from a custom UNION query combining two tables (users and groups). The list displays correctly, but when clicking a record in the table, it always returns the wrong model because some records share the same id. This causes Filament to resolve the wrong model in actions.

Is there a way to let Filament know which column should be unique (e.g., type + id) to correctly identify and resolve the model?

Here’s my getTableQuery:

private function getTableQuery(): Builder
{
    $customers = Customer::query()
        ->select([
            'id',
            'name',
            DB::raw("'customer' as type"),
        ])
        ->toBase();

    $groups = Groups::query()
        ->select([
            'id',
           'name',
            DB::raw("'group' as type"),
        ])
        ->toBase();

    // Union the two queries
    return $users->union($groups);
}
private function getTableQuery(): Builder
{
    $customers = Customer::query()
        ->select([
            'id',
            'name',
            DB::raw("'customer' as type"),
        ])
        ->toBase();

    $groups = Groups::query()
        ->select([
            'id',
           'name',
            DB::raw("'group' as type"),
        ])
        ->toBase();

    // Union the two queries
    return $users->union($groups);
}


In the table:

TextColumn::make('name')
    ->label('Name')
    ->sortable()
    ->searchable(),

Action::make('view')
    ->action(fn ($record) => dd($record)), // Always finds User instance even when it should be a Group
TextColumn::make('name')
    ->label('Name')
    ->sortable()
    ->searchable(),

Action::make('view')
    ->action(fn ($record) => dd($record)), // Always finds User instance even when it should be a Group
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

Filament table builder using union query
FilamentFFilament / ❓┊help
2y ago
hitting some limitations with Filament table builder only acceping query builder
FilamentFFilament / ❓┊help
2y ago
Table Query Builder with Constraints
FilamentFFilament / ❓┊help
13mo ago
Custom View Page using Filament Table Builder
FilamentFFilament / ❓┊help
3y ago