Filter's query returns collection but table stays empty

Hi everyone, I am experiencing strange behaviour from one of my filters. I have created a mix-max field, where I basically go and filter my records by minimum and maximum price. The query works through a normal when, with 2 different options: if a minimum price is entered and if a maximum price is entered. This way, when the maximum price was deleted from the input, the table returned nothing. So I thought of creating a third option: i.e. if the maximum price is null it returns all records with price <= the maximum possible price (obtained through a db query), but nothing has changed. I've done some dumps and actually the code enters the when and, doing a dump of $query, the where exists; doing a dump of $query->get() I get the records I expect (i.e. all of them); the problem is that anyway the table remains empty.
Solution:
I solved it by simplifying the callback within query, like this: ``` ->query(function (Builder $query, array $data): Builder { if($data['pricing']['max'] > 0){ $query = $query->where('price', '<=', $data['pricing']['max']);...
Jump to solution
2 Replies
Davide Cariola
Davide CariolaOP7mo ago
Here is my code:
'pricing' => Filter::make('pricing')
->form([
MinMaxField::make('pricing')
->label('Pricing (€)')
->default(['min' => null, 'max' => null])
->live()
])
->query(function (Builder $query, array $data): Builder {
return $query
->when(
isset($data['pricing']['min']),
fn (Builder $query) => $query->where('price', '>=', $data['pricing']['min']),
)
->when(
$data['pricing']['max'] !== null && $data['pricing']['max'] == null,
function (Builder $query) {
return $query->where('price', '<=', $this->maxPricePossible);
}
)
->when(
isset($data['pricing']['max']),
fn (Builder $query) => $query->where('price', '<=', $data['pricing']['max'] ?? $this->maxPricePossible),
);
}),
'pricing' => Filter::make('pricing')
->form([
MinMaxField::make('pricing')
->label('Pricing (€)')
->default(['min' => null, 'max' => null])
->live()
])
->query(function (Builder $query, array $data): Builder {
return $query
->when(
isset($data['pricing']['min']),
fn (Builder $query) => $query->where('price', '>=', $data['pricing']['min']),
)
->when(
$data['pricing']['max'] !== null && $data['pricing']['max'] == null,
function (Builder $query) {
return $query->where('price', '<=', $this->maxPricePossible);
}
)
->when(
isset($data['pricing']['max']),
fn (Builder $query) => $query->where('price', '<=', $data['pricing']['max'] ?? $this->maxPricePossible),
);
}),
Am I doing something wrong? Do you have any other ways to solve this? All I would need is for the table to display all the records once the input has been emptied of the maximum price.
Solution
Davide Cariola
Davide Cariola7mo ago
I solved it by simplifying the callback within query, like this:
->query(function (Builder $query, array $data): Builder {
if($data['pricing']['max'] > 0){
$query = $query->where('price', '<=', $data['pricing']['max']);
} else {
$query = $query->where('price', '<=', $this->maxPricePossible);
}

if($data['pricing']['min'] > 0){
dump($data['pricing']['min']);
$query = $query->where('price', '>=', $data['pricing']['min']);
} else {
$query = $query->where('price', '>=', 0);
}

return $query;
->query(function (Builder $query, array $data): Builder {
if($data['pricing']['max'] > 0){
$query = $query->where('price', '<=', $data['pricing']['max']);
} else {
$query = $query->where('price', '<=', $this->maxPricePossible);
}

if($data['pricing']['min'] > 0){
dump($data['pricing']['min']);
$query = $query->where('price', '>=', $data['pricing']['min']);
} else {
$query = $query->where('price', '>=', 0);
}

return $query;
Want results from more Discord servers?
Add your server