SelectFilter for multiple values in Json in field NOT WORKING

Hello. I have a field called TAGS where user can select multiple. I casted in the model:
protected $casts = [
'tags' => 'array',
];
protected $casts = [
'tags' => 'array',
];
And field is type: longText and stores in JSON like this: ["registros","otros"] when user selected those two values. I am trying the SelectFilter like this:
SelectFilter::make('tags')
->multiple()
->options([
'registros' => 'registros',
'plusvalías' => 'plusvalías',
'otros' => 'otros',
]),
SelectFilter::make('tags')
->multiple()
->options([
'registros' => 'registros',
'plusvalías' => 'plusvalías',
'otros' => 'otros',
]),
And nothing thows an error, and I can select one or more of those options, but NO DATA IS RETRIEVED. As it was a simple field, I did not create a pivot table or relation for this. Any ideas of how I can customize my filter to work?? Something like CONTAINS or WhereIn ? Thank you very much.
Solution:
After quite a lot time testing and looking for information and debugging the returned array, I have the solution working: ```php SelectFilter::make('tags') ->multiple()...
Jump to solution
2 Replies
Solution
Albert Lens
Albert Lens10mo ago
After quite a lot time testing and looking for information and debugging the returned array, I have the solution working:
SelectFilter::make('tags')
->multiple()
->options(self::$tagsOptions)
->query(function (Builder $query, array $data): Builder {
return (count($data['values'])==0) ? $query : $query->whereJsonContains('tags', $data['values']);
}),
SelectFilter::make('tags')
->multiple()
->options(self::$tagsOptions)
->query(function (Builder $query, array $data): Builder {
return (count($data['values'])==0) ? $query : $query->whereJsonContains('tags', $data['values']);
}),
Albert Lens
Albert Lens10mo ago
The solution is the query with the whereJsonContains(...)