Issue while generating complex query from multiple choices and multiple parameter filter

56 views Asked by At

Its an eCommerce app that has filters where parameters are taken dynamically from a table.

"Filters" table looks like following:

id | group | label | value
----+----------+-------------------+----------------

12 | deadline | 1 to 3 days | 1 AND 3
14 | budget | Any | 0
15 | budget | Less than 1000 | 1,1000
16 | budget | 1000 to 10000 | 1000, 10000
22 | location | Any | 0
23 | location | New York | newyork
24 | location | California | california
25 | location | Alabama | alabama

One of the parameters among many is: "Budget" and the checkbox options are:

All

1000 to 9999

10000 to 99999

100000 to 999999

Users can check multiple options here.

I'm trying to build search queries dynamically from this filter: (refer here https://m.dotdev.co/writing-advanced-eloquent-search-query-filters-de8b6c2598db)

View file for filters:

@foreach ($groups as $group => $filters)
                <div class="panel panel-default">
                    <b>{{ $group }}</b>
                    @foreach($filters as $filter)
                        <div id="collapseOne1" class="panel-collapse collapse in">
                            <div class="panel-body">
                                <div class="checkbox">
                                    <label><input class="
                                    <?php if ($filter->value == '0') { ?>
                                            {{ $filter->group }}
                                    <?php } else { ?>
                                                {{ $filter->group."Child" }}
                                    <?php }?>" type="checkbox" name="{{ $filter->group }}" value="{{ $filter->value }}">{{ $filter->label }}</label>
                                </div>
                            </div>
                        </div>
                    @endforeach
                </div>
       @endforeach

The controller has:

$requests = $requests->newQuery();
if ($request->has('budget')) {
        foreach (explode(',',$request->budget) as $budget) {
            $min = 0;
            $max = 1;
            if($budget < $min){
                $min = $budget;
            }
            if($budget > $max){
                $max = $budget;
            }
        }
            $requests->whereBetween('budget',[$min, $max]);
        }
$requests = $requests->paginate(5);
...

The above code doesn't seem to be working as it always sets min = 0. Can somebody tell how I can create a dynamic query out of a dynamically generated multi-choice filters in Laravel?

Thanks in advance,

1

There are 1 answers

1
jlos On

Unless the price is somehow negative, your $min will always be 0...? I don't fully understand what the problem is. You can chain queries together dynamically, but this seems to be logic problem, less than a query builder problem?

Edit based on the comment:

The problem is that $budget < $min will never return true, unless your $budget is somehow negative. 1000 < 0 => will return false, so it will never set $min to $budget. So you'll need to set a decent $min value. What you might wanna do is:

$min = $request->input('budget', 0);

This will assign $min to the budget parameter in your request, but if that parameter does not exist, it will default to 0.