F
Filament6mo ago
Watoka

Spatie tags filter

I am using Spatie tags in a resource, but if I am making a filter, it lists the available tags as {"en":"MyTag"}. Here's the code:
SelectFilter::make('tags')
->label('Tags')
->multiple()
->preload()
->forceSearchCaseInsensitive()
->relationship('tags', 'name'),
SelectFilter::make('tags')
->label('Tags')
->multiple()
->preload()
->forceSearchCaseInsensitive()
->relationship('tags', 'name'),
This was working fine in Filament v2. Probably there is a simple solution for this, but I could not figure it out.
24 Replies
lucianosnchz
lucianosnchz6mo ago
Hello ! Did you find the solution? thanks
Watoka
Watoka6mo ago
Not yet 😢
lucianosnchz
lucianosnchz6mo ago
😫 in v2 its work nobody report, if i find a solution i comment here thanks
lucianosnchz
lucianosnchz6mo ago
i delete this !! its work
No description
Watoka
Watoka6mo ago
Delete it from where? I did not overwrite getTagClassName
lucianosnchz
lucianosnchz6mo ago
I have Product.php (model) i add this code, i deleted and run if you want , share your code
lucianosnchz
lucianosnchz6mo ago
No description
lucianosnchz
lucianosnchz6mo ago
No description
Watoka
Watoka6mo ago
I just have a Contacts model:
class Contact extends Model
{
use HasFactory, HasTags
class Contact extends Model
{
use HasFactory, HasTags
lucianosnchz
lucianosnchz6mo ago
No description
Watoka
Watoka6mo ago
And this is the filter
No description
Watoka
Watoka6mo ago
And I don't have a tags() relationship, this should be handled automatically by the Spatie Tags plugin Works OK in the form, the problem is only in filters
lucianosnchz
lucianosnchz6mo ago
Do yo use SpatieTagInput on filters?
Watoka
Watoka6mo ago
No, just SelectFilter
SelectFilter::make('tags')
->label('Tags')
->multiple()
->preload()
->forceSearchCaseInsensitive()
->relationship('tags', 'name'),
SelectFilter::make('tags')
->label('Tags')
->multiple()
->preload()
->forceSearchCaseInsensitive()
->relationship('tags', 'name'),
This used to work in v2
DrByte
DrByte6mo ago
Question: if you choose one of those displayed filters, does it correctly perform the filtering? ie: is it just a display issue? or is the underlying query also not updating the table data?
Oh, and is the filter-indicator displayed as well?
Watoka
Watoka6mo ago
Yes, the filtering is performed correctly, but the indicator is wrong
Watoka
Watoka6mo ago
No description
swajp
swajp6mo ago
add this to your SelectFilter : ->getOptionLabelFromRecordUsing(fn($record) => $record['name'] )
Watoka
Watoka6mo ago
THanks, this is working. Kinda. The filter indicator is still broken.
Watoka
Watoka6mo ago
No description
swajp
swajp6mo ago
could you send me a code of your filter indicator?
Watoka
Watoka6mo ago
I don't have an indicator set up. THis is my code:
SelectFilter::make('tags')
->label('Tags')
->getOptionLabelFromRecordUsing(fn($record) => $record['name'] )
->multiple()
->preload()
->forceSearchCaseInsensitive()
->relationship('tags', 'name'),
SelectFilter::make('tags')
->label('Tags')
->getOptionLabelFromRecordUsing(fn($record) => $record['name'] )
->multiple()
->preload()
->forceSearchCaseInsensitive()
->relationship('tags', 'name'),
If I set an indicator, I don't have access to the $record, I get only the $data array which returns the id of the tag Of course I could make an extra query, but this was working fine in v2 I think I will submit a bug report
awcodes
awcodes6mo ago
Try $record->name. Translations in the spatie package only work when the attribute is called directly off the model. So $record[‘name’] won’t automatically translate.
bionary
bionary4w ago
I ran into these same issues today. @swajp @Watoka @awcodes This is what I'm doing to show the indicator:
Tables\Filters\SelectFilter::make('tags')
->multiple()
->preload(true)
->searchable()
->relationship(
name: 'tags',
titleAttribute: 'name',
)
->getOptionLabelFromRecordUsing(fn($record) => $record->name )
->indicateUsing(function (array $data){
$tags = Tag::whereIn('id', $data['values'])->get();
if($tags->count()) {
$tagsString = $tags->pluck('name')->implode(' & ');
return 'Tags: ' . $tagsString;
}
return null;
}),
Tables\Filters\SelectFilter::make('tags')
->multiple()
->preload(true)
->searchable()
->relationship(
name: 'tags',
titleAttribute: 'name',
)
->getOptionLabelFromRecordUsing(fn($record) => $record->name )
->indicateUsing(function (array $data){
$tags = Tag::whereIn('id', $data['values'])->get();
if($tags->count()) {
$tagsString = $tags->pluck('name')->implode(' & ');
return 'Tags: ' . $tagsString;
}
return null;
}),