Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

philipbaginski's avatar

Filter - request.

Welcome everybody! I'm facing this kind of problem:

I have a filter with request:

    /**
     * Apply the filter to the given query.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @param  mixed  $value
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function apply(Request $request, $query, $value)
    {
        return $query->where('allowable_age_id', '=', $value);
    }

    /**
     * Get the filter's available options.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function options(Request $request)
    {
        return [
            'Two' => '1',
            'Two & Up' => '2',
            'Three' => '3',
            'Three & Up' => '4',
            'Four' => '5',
            'Four & Up' => '6',
            'Five' => '7',
            'Five & Up' => '8',

        ];
    }

The values from options: Two, Three, Four, Five I would like to make equal to $value - what I already have, but for values from options: Two & Up, Three & Up, Four & Up, Five & Up I would like to include base value. E.g Three & Up should show records equal to 3 and all above.

Any idea how to solve this kind of problem?

0 likes
21 replies
sr57's avatar
sr57
Best Answer
Level 39

in your function apply

1-create different several return depending if $value

or

2-create an array min,max depending with key = $value

and

make a return where >= min and <=max

1 like
philipbaginski's avatar

Something like that:

if ($value = 3)
        {
            return $query->where('allowable_age_id', '=', $value);
        }

sr57's avatar

Yes and what you want after

else if ($value = 4)
        {
            return $query->where('allowable_age_id', '>=', 3);
        }
...

You can do all what you want with such solution.

philipbaginski's avatar

I tried with code below:

    public function apply(Request $request, $query, $value)
    {
        if ($value = 3)
        {
            return $query->where('allowable_age_id', '=', $value);
        }
        else if ($value = 4)
        {
            return $query->where('allowable_age_id', '>=', 3);
        }

    }

, and it does not work.

sr57's avatar

what about the other values?

at least add a final else that returns the query without filter ...

philipbaginski's avatar
    public function apply(Request $request, $query, $value)
    {
        if ($value = 3)
        {
            return $query->where('allowable_age_id', '=', $value);
        }
        else
        {
            return $query->where('allowable_age_id', '>=', $value);
        }
    }

Returns only records with 'allowable_age_id' = 3 :-(

sr57's avatar

Returns only records with 'allowable_age_id' = 3

! ? No sense you have more than one case ... and you have to adapt your tests to your your data.

philipbaginski's avatar

I think is not as easy as it looks. No idea why it returns the same values.

sr57's avatar

it returns the same values

What tests did you try? (inputs / output of your function)

What are the values of 'allowable_age_id' ?

philipbaginski's avatar

ID's - from 1 to 8.

I found the reason the test does not work:

instead of $value = 3 should be $value == 3

I should repeat "if" or make one time if and the rest cases use "else"?

sr57's avatar

if

else if

else if

...

else : final with no filter to avoid error for forgotten case(s)

1 like
philipbaginski's avatar

Final cut:

    public function apply(Request $request, $query, $value)
    {
        if ($value == 1)
        {
            return $query->where('allowable_age_id', '=', $value);
        }
        else if ($value == 2)
        {
            return $query->where('allowable_age_id', '>=', $value -1);
        }
        else if ($value == 3)
        {
            return $query->where('allowable_age_id', '=', $value);
        }
        else if ($value == 4)
        {
            return $query->where('allowable_age_id', '>=', $value -1);
        }
        else if ($value == 5)
        {
            return $query->where('allowable_age_id', '=', $value);
        }
        else if ($value == 6)
        {
            return $query->where('allowable_age_id', '>=', $value -1);
        }
        else if ($value == 7)
        {
            return $query->where('allowable_age_id', '=', $value);
        }
        else if ($value == 8)
        {
            return $query->where('allowable_age_id', '>=', $value -1);
        }
        else
        {
            return $query->where('allowable_age_id', '=', $value);
        }
    }

sr57's avatar

Does the result is what you expect?

Personnally I should change the final else

  • no filter : return $query

or

  • no result : retrun $query->where('allowable_age_id', '=', -1);
philipbaginski's avatar

Yes, it work like never before :-)

There is no way, that in column 'allowable_age_id' is no value. But if, how to protect this query for returning 'no-object' what means: no value?

sr57's avatar

A little bit long

You could reduce the code but it's often better to have a code easy to read/understand and code depends of your initial data.

1 like
sr57's avatar

for returning 'no-object' what means: no value?

Not with the filter, but if your view you can test if null and echo the message you want.

1 like
philipbaginski's avatar

One question more. I found logical bug in the way I'm filtering results. The options are:

    public function options(Request $request)
    {
        return [
            'Two' => '1',
            'Two & Up' => '2',
            'Three' => '3',
            'Three & Up' => '4',
            'Four' => '5',
            'Four & Up' => '6',
            'Five' => '7',
            'Five & Up' => '8',

        ];
    }

Base values are fine, what means, that Two, Three, Four and Five will show only results equal to the choosen value.

But extended values: Two & Up, Three & Up and Four & Up can not include Three, Four and Five results.

Eg. If I will choose Two & Up, it should show Three & Up, Four & Up and Five & Up results, without Three, Four and Five results.

sr57's avatar

Don't undesrtand.

If you choose Two & Up, value is 2, and test is >=1 means all >= Two.

Either a misundestanding between us or a pb of test with integer and chr?

philipbaginski's avatar

Sorry, the mistake is mine. I have three ways of showing results and it's more about the metodology, less about the bug in the code.

  1. If I choose Three & Up (value 4) I may see only results with value 4 from the column allowable_age_id.
  2. If I choose Three & Up (value 4) I may see results with value 3 (Three which is included in Three & Up) and ALL above (up to 8) from the column allowable_age_id.
  3. If I choose Three & Up (value 4) I may see results with value 3 (Three which is included in Three & Up) and above from the column allowable_age_id, excluded results linked with higher base values: 5, 7.

Base values are restricted to itself.

Method whereIn works perfect for this case. But I have to choose the metodology I should to use to filter this table.

else if ($value == 6)
        {
            return $query->whereIn('allowable_age_id', [5, 6, 8]);
        }

Thanks one time more for helping me. The problem is fully solved and even more, the solution created too many possibilities :-)

sr57's avatar

Nice to know, it's what I wrote at the beginning wit this solution you can adapt your tests to exactly what you need and that's what you do :-)

1 like

Please or to participate in this conversation.