Eager loading relationships in table

I currently have this piece of code:
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('last_name')
->label(__('labels.name'))
->getStateUsing(fn(Volunteer $record) => $record->person->fullName())
->sortable()
->searchable(['person.first_name', 'person.middle_name', 'person.last_name']),
Tables\Columns\TextColumn::make('person.email')
->label(__('labels.email'))
->sortable()
->searchable(),
Tables\Columns\TextColumn::make('function')
->label(__('labels.function'))
->sortable()
->searchable(),
])
->modifyQueryUsing(fn(Builder $query) => $query->with('person'))
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('last_name')
->label(__('labels.name'))
->getStateUsing(fn(Volunteer $record) => $record->person->fullName())
->sortable()
->searchable(['person.first_name', 'person.middle_name', 'person.last_name']),
Tables\Columns\TextColumn::make('person.email')
->label(__('labels.email'))
->sortable()
->searchable(),
Tables\Columns\TextColumn::make('function')
->label(__('labels.function'))
->sortable()
->searchable(),
])
->modifyQueryUsing(fn(Builder $query) => $query->with('person'))
When trying to search i get the error "SQLSTATE[42P01]: Undefined table: 7 ERROR: missing FROM-clause entry for table "person" LINE 1: ...nt(*) as aggregate from "volunteers" where (lower(person.fir... ^" Does anyone have an insight in how i might fix this? I thought the $query->with() would already solve it but unfortuantly it doesn't
Solution:
Can you try using person.lastname as the name so that filament knows it’s a relation? πŸ€”
Jump to solution
9 Replies
Dennis Koch
Dennis Kochβ€’4w ago
Can you share the whole error or the stack trace?
DanielvdSpoel
DanielvdSpoelβ€’4w ago
Flare
SQLSTATE[42P01]: Undefined table: 7 ERROR: missing FROM-clause entry for table "person" LINE 1: ...t() as aggregate from "care_takers" where (lower(person.fir... ^ (Connection: pgsql, SQL: select count() as aggregate from "care_takers" where (lower(person.first_name::text)::text lik...
Dennis Koch
Dennis Kochβ€’4w ago
And the DB table for person is person? Or is it people? Then you should adjust the searchable
DanielvdSpoel
DanielvdSpoelβ€’4w ago
Oh my πŸ˜‚ Going to have to check, but your propably right
DanielvdSpoel
DanielvdSpoelβ€’4w ago
don't think that fixes anything? https://flareapp.io/share/A7E8rdBP
return $table
->columns([
Tables\Columns\TextColumn::make('last_name')
->label(__('labels.name'))
->getStateUsing(fn(Volunteer $record) => $record->person->fullName())
->sortable()
->searchable(['people.first_name', 'people.middle_name', 'people.last_name']),
Tables\Columns\TextColumn::make('person.email')
->label(__('labels.email'))
->sortable()
->searchable(),
Tables\Columns\TextColumn::make('function')
->label(__('labels.function'))
->sortable()
->searchable(),
])
->modifyQueryUsing(fn(Builder $query) => $query->with('person'))
return $table
->columns([
Tables\Columns\TextColumn::make('last_name')
->label(__('labels.name'))
->getStateUsing(fn(Volunteer $record) => $record->person->fullName())
->sortable()
->searchable(['people.first_name', 'people.middle_name', 'people.last_name']),
Tables\Columns\TextColumn::make('person.email')
->label(__('labels.email'))
->sortable()
->searchable(),
Tables\Columns\TextColumn::make('function')
->label(__('labels.function'))
->sortable()
->searchable(),
])
->modifyQueryUsing(fn(Builder $query) => $query->with('person'))
Flare
SQLSTATE[42P01]: Undefined table: 7 ERROR: missing FROM-clause entry for table "people" LINE 1: ...nt() as aggregate from "volunteers" where (lower(people.fir... ^ (Connection: pgsql, SQL: select count() as aggregate from "volunteers" where (lower(people.first_name::text)::text like...
DanielvdSpoel
DanielvdSpoelβ€’4w ago
Bumb, can provide bounty of $10 dollars if solved
Solution
Dennis Koch
Dennis Kochβ€’4w ago
Can you try using person.lastname as the name so that filament knows it’s a relation? πŸ€”
DanielvdSpoel
DanielvdSpoelβ€’4w ago
your a genius! send some money on paypal
Dennis Koch
Dennis Kochβ€’4w ago
Thanks. Appreciate it!