I need SelectFilter with no relationship to show null values to choose from

Hello. I have a resource with a field called 'group' which is NOT a relationship and user can type whatever they want (string). And I need o add a filter to let them choose from all the different variations in the column. I already get a good result with that, but I do not know hot to ADD the option to choose THE NULL ones, I mean, if you choose group 'IV' it works, but I can not choose "blank ones" o "unset" (regarding the column 'group' which is nullable) My filter at the moment is:
SelectFilter::make('group')
->multiple()
->options([
RelationshipType::all('group')->sortBy('group')->pluck('group', 'group')->toArray(),
]),
SelectFilter::make('group')
->multiple()
->options([
RelationshipType::all('group')->sortBy('group')->pluck('group', 'group')->toArray(),
]),
It also shows strange valued such as 0 (Zero) but no record has a Zero in that column. Any ideas of how to make a filter to let user choose from any differente value in one column including a filter for the unset o null ones? Tks.
6 Replies
Noxo
Noxo16mo ago
Because you pass array in array. remove these brackets ->options([ must be like this -
SelectFilter::make('group')
->multiple()
->options(
RelationshipType::all('group')->sortBy('group')->pluck('group', 'group')->toArray(),
),
SelectFilter::make('group')
->multiple()
->options(
RelationshipType::all('group')->sortBy('group')->pluck('group', 'group')->toArray(),
),
also you can write it shortly
RelationshipType::orderBy('group')->pluck('group', 'group')->toArray()
RelationshipType::orderBy('group')->pluck('group', 'group')->toArray()
Albert Lens
Albert LensOP16mo ago
Thank you for the tip. I removed the first pair or brackets and now it shows all values correctly without strange zeros above. But, anyway, I would like to have the unset ones (the null ones) to choose from. I mean, if there are three variations in the records (1,2,3) but there are also records with a blank value in that field, I would like to be able to choose the blank ones also, besides every different value. Is that possible?
Noxo
Noxo16mo ago
aren't empty records currently in the select options?
Albert Lens
Albert LensOP16mo ago
No Anyway, I have thought of a way to change the content of the column called 'group' retrieving the group if there is anything written or 'empty' if it is null. That way I could filter by all the values including the empty string value. But when I change the getEloquentQuery(): Builder it throws an error ==> Return value must be of type Illuminate\Database\Eloquent\Builder, Illuminate\Support\Collection returned My code is:
public static function getEloquentQuery(): Builder
{

$array=parent::getEloquentQuery()
->withoutGlobalScopes([
SoftDeletingScope::class,
])->get()->map(function($item){
$item['group']= ($item['group'] <> null) ? $item['group'] : 'botijo';
// $item['group']= ($item['group'] <> null) ? $item['group'] : null);
});
return $array;
}
}
public static function getEloquentQuery(): Builder
{

$array=parent::getEloquentQuery()
->withoutGlobalScopes([
SoftDeletingScope::class,
])->get()->map(function($item){
$item['group']= ($item['group'] <> null) ? $item['group'] : 'botijo';
// $item['group']= ($item['group'] <> null) ? $item['group'] : null);
});
return $array;
}
}
How can I do that??? I would need to change the query (the column value with a ternary if statement). Tks. Now, with this function:
public function getAttributeValue($key)
{
if ($key == 'group') {
return ($this->attributes['group'] <> null) ? $this->attributes['group'] : 'empty';
}
return parent::getAttributeValue($key);
}
public function getAttributeValue($key)
{
if ($key == 'group') {
return ($this->attributes['group'] <> null) ? $this->attributes['group'] : 'empty';
}
return parent::getAttributeValue($key);
}
I can change the column data when it is retrieved, which is fantastic, but nothing happens in the filter.
Noxo
Noxo16mo ago
The getEloquentQuery method only to modify the query. Use the formatStateUsing method on your column - https://filamentphp.com/docs/3.x/tables/columns/text#custom-formatting Examples:
TextColumn::make('group')
->formatStateUsing(fn (?string $state): string => $state <> null ? $state : 'botijo')
TextColumn::make('group')
->formatStateUsing(fn (?string $state): string => $state <> null ? $state : 'botijo')
TextColumn::make('group')
->formatStateUsing(fn (?string $state): string => $state ?? 'botijo')
TextColumn::make('group')
->formatStateUsing(fn (?string $state): string => $state ?? 'botijo')
Albert Lens
Albert LensOP16mo ago
Right. Thank you. But then the filter is not aware of this changes, so I still cannot filter by the null ones, and not even the search is aware of these changes.
Want results from more Discord servers?
Add your server