Adding a SelectFilter with relationship breaks table ordering

I think this is a standard filament question but I'm using the plugin filament-auditing https://filamentphp.com/plugins/auditing and have extended the AuditsRelationManager to add a users filter. https://github.com/TappNetwork/filament-auditing/blob/main/src/RelationManagers/AuditsRelationManager.php
->filters([
Tables\Filters\SelectFilter::make('user_id')
->relationship('user', 'name')
])
->filters([
Tables\Filters\SelectFilter::make('user_id')
->relationship('user', 'name')
])
But not sure why when I add this filter I get
Column not found: 1054 Unknown column 'name' in 'field list' (Connection: mysql, SQL: select name, audits.` from audits order by name` asc)
2 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
gizmojo
gizmojoOP2y ago
Cheers but that didn't work. But I think you're right because the user is a polymorphic relationship is why the select doesn't work. I'm so used to it being a belongsTo. I ended up doing it manually
Tables\Filters\Filter::make('user_id')
->form([
Select::make('user_id')
->options(fn () => User::orderBy('name')->pluck('name', 'id')->toArray())
])
->query(function (Builder $query, array $data) {
$query->when($data['user_id'], fn ($query) =>
$query->where('user_id', (int) $data['user_id'])->where('user_type', 'user')
);
})
->indicateUsing(function (array $data): array {
$indicators = [];

if (! empty($data['user_id'])) {
$indicators['user_id'] = __('filter.user', ['name' => User::find($data['user_id'], 'name')->value('name')]);
}

return $indicators;
}),
Tables\Filters\Filter::make('user_id')
->form([
Select::make('user_id')
->options(fn () => User::orderBy('name')->pluck('name', 'id')->toArray())
])
->query(function (Builder $query, array $data) {
$query->when($data['user_id'], fn ($query) =>
$query->where('user_id', (int) $data['user_id'])->where('user_type', 'user')
);
})
->indicateUsing(function (array $data): array {
$indicators = [];

if (! empty($data['user_id'])) {
$indicators['user_id'] = __('filter.user', ['name' => User::find($data['user_id'], 'name')->value('name')]);
}

return $indicators;
}),
Want results from more Discord servers?
Add your server