FilamentF
Filament14mo ago
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); }

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
Was this page helpful?