withTrashed eagerload doesnt work in table columns

So i have this public static function getEloquentQuery() { return parent::getEloquentQuery() ->with('relation', fn ($query) => $query->withTrashed()); }
8 Replies
Ricardo Sawir
Ricardo SawirOP2y ago
it doesnt work
toeknee
toeknee2y ago
Is this in admin or in tables seperately? Why are you applying trashed in the Eloquent query? Add a trashed filter and apply it by default so people can filter trashed and non-trashed etc
toeknee
toeknee2y ago
Filament
Filters - Table Builder - Filament
The elegant TALL stack table builder for Laravel artisans.
Dan Harrin
Dan Harrin2y ago
add withTrashed() on to the relationship definition itself this is for a relationship, not the main table query
toeknee
toeknee2y ago
Ahh I missed that
Ricardo Sawir
Ricardo SawirOP2y ago
isn't my code what you were referring to?
public static function getEloquentQuery()
{
return parent::getEloquentQuery()
->with('relation', fn ($query) => $query->withTrashed());
}
public static function getEloquentQuery()
{
return parent::getEloquentQuery()
->with('relation', fn ($query) => $query->withTrashed());
}
the same code works if I use it for non table (e.g dumping) should not be related with the applyEagerLoads method right? Worth to note, that I also have
Forms\Components\TextColumn::make('relation.name'),
Forms\Components\TextColumn::make('relation.code'),
Forms\Components\TextColumn::make('relation.name'),
Forms\Components\TextColumn::make('relation.code'),
does this have any association with withTrashed not working?
Dan Harrin
Dan Harrin2y ago
no, the relationship definition method on your model not the table query
Ricardo Sawir
Ricardo SawirOP2y ago
noted... do you know why the table query doesnt work while the relationship definition does? I did an experiment, using these
Forms\Components\TextColumn::make('relation.name'),
Forms\Components\TextColumn::make('relation.code'),
Forms\Components\TextColumn::make('relation.name'),
Forms\Components\TextColumn::make('relation.code'),
does work with what you propose the relationship definition method on your model however, if experiment with these:
Forms\Components\TextColumn::make('relationName')
->formatStateUsing(fn ($record) => $record->relation->name),
Forms\Components\TextColumn::make('relationCode')
->formatStateUsing(fn ($record) => $record->relation->code),

// and using getEloquentQuery()
public static function getEloquentQuery()
{
return parent::getEloquentQuery()
->with('relation', fn ($query) => $query->withTrashed());
}
Forms\Components\TextColumn::make('relationName')
->formatStateUsing(fn ($record) => $record->relation->name),
Forms\Components\TextColumn::make('relationCode')
->formatStateUsing(fn ($record) => $record->relation->code),

// and using getEloquentQuery()
public static function getEloquentQuery()
{
return parent::getEloquentQuery()
->with('relation', fn ($query) => $query->withTrashed());
}
Want results from more Discord servers?
Add your server