F
Filament12mo 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
lucianosnchz12mo ago
Hello ! Did you find the solution? thanks
Watoka
WatokaOP12mo ago
Not yet 😢
lucianosnchz
lucianosnchz12mo ago
😫 in v2 its work nobody report, if i find a solution i comment here thanks
lucianosnchz
lucianosnchz12mo ago
i delete this !! its work
No description
Watoka
WatokaOP12mo ago
Delete it from where? I did not overwrite getTagClassName
lucianosnchz
lucianosnchz12mo ago
I have Product.php (model) i add this code, i deleted and run if you want , share your code
lucianosnchz
lucianosnchz12mo ago
No description
lucianosnchz
lucianosnchz12mo ago
No description
Watoka
WatokaOP12mo ago
I just have a Contacts model:
class Contact extends Model
{
use HasFactory, HasTags
class Contact extends Model
{
use HasFactory, HasTags
lucianosnchz
lucianosnchz12mo ago
No description
Watoka
WatokaOP12mo ago
And this is the filter
No description
Watoka
WatokaOP12mo 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
lucianosnchz12mo ago
Do yo use SpatieTagInput on filters?
Watoka
WatokaOP12mo 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
DrByte12mo 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
WatokaOP12mo ago
Yes, the filtering is performed correctly, but the indicator is wrong
Watoka
WatokaOP12mo ago
No description
swajp
swajp12mo ago
add this to your SelectFilter : ->getOptionLabelFromRecordUsing(fn($record) => $record['name'] )
Watoka
WatokaOP12mo ago
THanks, this is working. Kinda. The filter indicator is still broken.
Watoka
WatokaOP12mo ago
No description
swajp
swajp12mo ago
could you send me a code of your filter indicator?
Watoka
WatokaOP12mo 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
awcodes12mo 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
bionary7mo 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;
}),
Want results from more Discord servers?
Add your server