SelectFilter->getOptionLabelFromRecordUsing

SelectFilter::make('event')
->options(fn (Table $table) => $table->getQuery()->pluck('event', 'event')->unique()->sort())
->searchable()
->preload(),
SelectFilter::make('user')
->options(fn (Table $table) => $table->getQuery()->pluck('actor_id', 'actor_id')->unique()->sort())
->getOptionLabelFromRecordUsing(function (int $actor) {
$user = User::query()->where('id', $actor)?->username;
if (!$user instanceof User) {
return 'System';
}

return $user->username;
})
->searchable()
->preload(),
SelectFilter::make('event')
->options(fn (Table $table) => $table->getQuery()->pluck('event', 'event')->unique()->sort())
->searchable()
->preload(),
SelectFilter::make('user')
->options(fn (Table $table) => $table->getQuery()->pluck('actor_id', 'actor_id')->unique()->sort())
->getOptionLabelFromRecordUsing(function (int $actor) {
$user = User::query()->where('id', $actor)?->username;
if (!$user instanceof User) {
return 'System';
}

return $user->username;
})
->searchable()
->preload(),
The label isn't applied either I even tried to dd() in getOptionLabelFromRecordUsing() but it doesn't get called at all
No description
No description
Solution:
Likely because your code is wrong. $actor won't exist... Why not do: ```php...
Jump to solution
2 Replies
Solution
toeknee
toeknee4w ago
Likely because your code is wrong. $actor won't exist... Why not do:
SelectFilter::make('user')
->relationship('user', 'username')
->searchable()
->preload(),
SelectFilter::make('user')
->relationship('user', 'username')
->searchable()
->preload(),
Providing the model of the resource has the user relationship?
Martin Oscar
Martin OscarOP4w ago
That may be the correct way instead of querying a pivot table each time ty 👌

Did you find this page helpful?