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

swapnilmanew's avatar

how to use date filter with other search filter in laravel

I want to filter the data between dates. I have so many columns from the front end.

I want to filter the data with other search queries. for example I have companies list. so I want to find 2000 to 2020 Active Companies in particular country.

I have coded this

 $data = tbl_company::query()->where(function ($query) use ($req) {
            foreach ($req->only('state', 'district', 'city', 'company_status', 'activity_code', 'company_category', 'company_class', 'company_type', 'office_type', 'date_of_registration') as $filterField => $filterFieldValue) {
                if ($req['date_of_registration']['start'] && $req['date_of_registration']['end']) {
                    $from = Carbon::parse($req['date_of_registration']['start']);
                    $to = Carbon::parse($req['date_of_registration']['end']);
                    $query->whereBetween(
                        DB::Raw("STR_TO_DATE(date_of_registration,'%d-%m-%Y')"),
                        [$from, $to]
                    );
                } elseif (!empty($filterFieldValue) && is_array($filterFieldValue)) {
                    $query->wherein($filterField, $filterFieldValue);
                } elseif (!empty($filterFieldValue)) {
                    $query->where($filterField, "LIKE", "%{$filterFieldValue}%");
                }
            }
        })->paginate(100);

but this only filters either the date or the other data like active, inactive companies.

0 likes
6 replies
Sinnbeck's avatar

Please start by formatting your code by adding ``` on the line before and after it

1 like
OussamaMater's avatar
Level 37

A question very similar to this one was asked yesterday, and I still recommend the pipeline pattern for this kind of filtering, you need to make separate filters, so you can scale the code later, in case you want to add 10 more filters for example.

Link to the question:

Link to the tutorial that will get you started:

In separate filters, you will keep the same condition which is, if X is set then add it, otherwise ignore it (X refers a random input).

2 likes
swapnilmanew's avatar

@OussamaMater It might be what I am looking for but can you please correct my code or provide some code snippet? These values are coming from the request

'state', 'district', 'city', 'company_status', 'activity_code', 'company_category', 'company_class', 'company_type', 'office_type', 'date_of_registration'

model = tbl_company
swapnilmanew's avatar

@OussamaMater Thank you so much, sir. The pipeline design pattern worked so well. I am so thankful for your response. It worked so super! I have been struggling for the last 3 days.

Please or to participate in this conversation.