Please start by formatting your code by adding ``` on the line before and after it
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.
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).
Please or to participate in this conversation.