Filters not returning data in query when using ->multiple()

I have the following filters for my table:
->filters([
SelectFilter::make('author_id')
->default(fn () => Auth::id())
->options(fn () => $userModel::all()->pluck('name', 'id'))
->query(fn (Builder $query, $data): Builder => dd($data))
->preload(),
SelectFilter::make('collaborators_id')
->default(fn () => Auth::id())
->options(fn () => $userModel::all()->pluck('name', 'id'))
->query(fn (Builder $query, $data): Builder => dd($data))
->multiple()
->preload(),
])
->filters([
SelectFilter::make('author_id')
->default(fn () => Auth::id())
->options(fn () => $userModel::all()->pluck('name', 'id'))
->query(fn (Builder $query, $data): Builder => dd($data))
->preload(),
SelectFilter::make('collaborators_id')
->default(fn () => Auth::id())
->options(fn () => $userModel::all()->pluck('name', 'id'))
->query(fn (Builder $query, $data): Builder => dd($data))
->multiple()
->preload(),
])
As you can see the second filter has multiple, but for some reason, when trying to dd the default record in the query, I get an empty array?
No description
14 Replies
Matthew
MatthewOP4d ago
But, if I dont have multiple (like in the first filter), then it returns the id (as it should)
No description
LeandroFerreira
try ->default([auth()->id()])
Matthew
MatthewOP4d ago
SelectFilter::make('collaborators_id')
->default([auth()->id()])
->options(fn () => $userModel::all()->pluck('name', 'id'))
// ->query(fn (Builder $query, $data): Builder => dd($data))
->preload(),
SelectFilter::make('collaborators_id')
->default([auth()->id()])
->options(fn () => $userModel::all()->pluck('name', 'id'))
// ->query(fn (Builder $query, $data): Builder => dd($data))
->preload(),
array_key_exists(): Argument #1 ($key) must be a valid array offset type I should mention, that the collaborators_id column is a json If you want to know more about how Ive set up the collaborators: here @Bruno Pereira no, it doesnt make a difference
Bruno Pereira
Bruno Pereira4d ago
yeah I noticed thats why I deleted the comment xD
Matthew
MatthewOP4d ago
ahaha It definitely has to do with the way Im processing the json data
Bruno Pereira
Bruno Pereira4d ago
collaborators_id is being stored as a json?
Matthew
MatthewOP4d ago
yess You can check the repo here
Bruno Pereira
Bruno Pereira4d ago
->query(fn (Builder $query, $data): Builder => $query->whereJsonContains('collaborators_id',$data)) ? just went to the docs how to query json using Query Builder, don't know if it works :/
Matthew
MatthewOP4d ago
The issue atm is that I cant set a value as default
Bruno Pereira
Bruno Pereira4d ago
->default($userModel::where('id',auth()->id())->first()->pluck('name', 'id')) ? or in a closure
Matthew
MatthewOP4d ago
nope :/, still the same I will ask copilot 😅
Bruno Pereira
Bruno Pereira4d ago
damn it. Best of luck and share the solution if you find it 🙂
Matthew
MatthewOP4d ago
I have made some progress, however I have a very weird issue, for some reason, no matter what default values wont be applied. For example https://filamentphp.com/docs/3.x/tables/filters/select#applying-select-filters-by-default I put this in my filters array as a test:
SelectFilter::make('status')
->options([
'draft' => 'Draft',
'reviewing' => 'Reviewing',
'published' => 'Published',
])
->default('draft')
SelectFilter::make('status')
->options([
'draft' => 'Draft',
'reviewing' => 'Reviewing',
'published' => 'Published',
])
->default('draft')
And draft wasnt applied as a default value @Bruno Pereira This is my solution
SelectFilter::make('author_id')
->label(__('filament-latex::filament-latex.column.author.name'))
->default(fn () => Auth::id())
->options(fn () => $userModel::all()->pluck('name', 'id'))
->native(false),
SelectFilter::make('collaborators_id')
->label(__('filament-latex::filament-latex.column.collaborators'))
->default(fn () => [Auth::id()])
->options(fn () => $userModel::all()->pluck('name', 'id'))
->query(function ($query, $data) {
if (! empty($data)) {
// Apply the filter for JSON column
foreach ($data as $id) {
$query->whereJsonContains('collaborators_id', $id);
}
}
})
->multiple()
->native(false),
SelectFilter::make('author_id')
->label(__('filament-latex::filament-latex.column.author.name'))
->default(fn () => Auth::id())
->options(fn () => $userModel::all()->pluck('name', 'id'))
->native(false),
SelectFilter::make('collaborators_id')
->label(__('filament-latex::filament-latex.column.collaborators'))
->default(fn () => [Auth::id()])
->options(fn () => $userModel::all()->pluck('name', 'id'))
->query(function ($query, $data) {
if (! empty($data)) {
// Apply the filter for JSON column
foreach ($data as $id) {
$query->whereJsonContains('collaborators_id', $id);
}
}
})
->multiple()
->native(false),
Bruno Pereira
Bruno Pereira4d ago
Oh I see, in this you're not preloading the data, but initially you were, maybe that was the problem because the builder wasnt getting any data.

Did you find this page helpful?