F
Filament11mo ago
Wirkhof

Unable to have Tab results based on a multi-select json column

In the DB I have a table cars with a column brand with multi-select values like: ["4","19"]. I have this in my ListCars.php file:
public function getTabs(): array
{

$returner = [];

$allBrands = Brand::all();

foreach ($allBrands as $i) {
$returner[$i->brand_name] = Tab::make()
->modifyQueryUsing(fn (Builder $query) => $query->whereIn('brand', [$i->id]));
}

return $returner;
}
public function getTabs(): array
{

$returner = [];

$allBrands = Brand::all();

foreach ($allBrands as $i) {
$returner[$i->brand_name] = Tab::make()
->modifyQueryUsing(fn (Builder $query) => $query->whereIn('brand', [$i->id]));
}

return $returner;
}
But I am not getting any results. If I manually change the ["4","19"] in the db table column to e.g. just 1 item with the number 4 it will work. It seems that whereIn condition is bad? How to make it work with multiselect values stored in the DB?
1 Reply
Wirkhof
WirkhofOP11mo ago
I am using this cast in the model for cars:
protected $casts = [
'brand' => 'array'
];
protected $casts = [
'brand' => 'array'
];
I have also tried:
protected $casts = [
'brand' => 'json'
];
protected $casts = [
'brand' => 'json'
];
But it is the same, doesn't work. No results in tabs. I have noticed that if I select just 1 item in the form it save it as ["5"] and not just 5 in the DB. Could this be a problem? If I manually change it from ["5"] to just 5 then the tab filtering will work. But it won't be picked up in the Edit page form field for the multi-select. Multi-select will work correctly only with ["5"] or ["5","21"], etc. but not just 5 I really think that the whereIn part: ->whereIn('brand', [$i->id])) is not correct. But have no idea how to fix it. Thanks for any help. In the end, I have bruteforced it via "like" but if anybody knows about a better solution, please, let me know.
Want results from more Discord servers?
Add your server