I want to filter the data between dates. I have so many columns from the front end.
Screenshot of front end columns
I have the following code that works fine unless the date filter comes. How can I add the date filter in this code and that will work.
I have provided the blade file code and the controller's function code below.
** BLADE FILE**
<form action="/filter/companies/data" method="get" >
<h5 class="text-center">Basic Filters</h5>
<input type="text" name="company_name" id="" placeholder="Company Name" class="form-control">
<input type="text" name="cin" id="" placeholder="CIN" class="form-control my-2">
<div class="form-group">
<label for="">Company Status</label>
<select class="select2 select2-hidden-accessible" multiple="" data-placeholder="Company Status" style="width: 100%;" tabindex="-1" name="company_status[]" aria-hidden="true">
<option value="Active">Active</option>
<option value="Inactive ">Inactive </option>
</select>
</div>
<div class="form-group">
<label for="">Company Category</label>
<select class="select2 select2-hidden-accessible" multiple="" data-placeholder="Company Category" style="width: 100%;" tabindex="-1" name="company_category[]" aria-hidden="true">
@foreach($company_category2 as $key => $item)
<option value="{{$item}}">{{$item}}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="">Activity Code</label>
<select class="select2 select2-hidden-accessible" multiple="" data-placeholder="Activity Code" style="width: 100%;" tabindex="-1" name="activity_code[]" aria-hidden="true">
@foreach($activity_code2 as $key => $item)
<option value="{{$item}}">{{$item}}</option>
@endforeach
</select>
</div>
<input type="text" name="roc" id="" placeholder="ROC" class="form-control my-2">
<hr/>
<h5 class="text-center">Company Details</h5>
<div class="form-group">
<label for="">Company Class</label>
<select class="select2 select2-hidden-accessible" multiple="" data-placeholder="Company Class" style="width: 100%;" tabindex="-1" name="company_class[]" aria-hidden="true">
<option value="Public Company">Public Company</option>
<option value="Private Company">Private Company</option>
</select>
</div>
<div class="form-group">
<label for="">Company Type</label>
<select class="select2 select2-hidden-accessible" multiple="" data-placeholder="Company Type" style="width: 100%;" name="company_type[]" aria-hidden="true">
@foreach($company_type2 as $key => $item)
<option value="{{$item}}">{{$item}}</option>
@endforeach
</select>
</div>
<input type="text" name="office_type" id="" placeholder="Office Type" class="form-control my-2">
<input type="text" name="llpin" id="" placeholder="LLP IN" class="form-control my-2">
<input type="text" name="llp_name" id="" placeholder="LLP NAME" class="form-control my-2">
<input type="text" name="industrial_activity" id="" placeholder="Industrial Activity" class="form-control my-2">
<hr/>
<h5 class="text-center">Filter using Contact Details</h5>
<input type="text" name="email" id="" placeholder="Email" class="form-control mb-2">
<input type="text" name="mobile_number" id="" placeholder="Mobile Number" class="form-control my-2">
<hr/>
<h5 class="text-center">Filter using Authorized Capital</h5>
<input type="text" name="from_authorized_capital" id="" placeholder="From Authorized Capital" class="form-control mb-2">
<input type="text" name="to_authorized_capital" id="" placeholder="To Authorized Capital" class="form-control my-2">
<hr/>
<h5 class="text-center">Filter using Locations</h5>
<div class="form-group">
<label for="">Select State</label>
<select class="select2 select2-hidden-accessible" multiple="" data-placeholder="Select a State" style="width: 100%;" tabindex="-1" name="state[]" aria-hidden="true">
@foreach($states as $key => $item)
<option value="{{$item}}">{{$item}}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="">Select District</label>
<select class="select2 select2-hidden-accessible" multiple="" data-placeholder="Select a District" style="width: 100%;" tabindex="-1" name="district[]" aria-hidden="true">
@foreach($districts as $key => $item)
<option value="{{$item}}">{{$item}}</option>
@endforeach
</select>
</div>
<input type="text" name="city" id="" placeholder="City" class="form-control my-2">
<hr/>
<h5 class="text-center">Dates Of Registration</h5>
<label for="">Start Date</label>
<input type="date" name="date_of_registration[start]" id="" class="form-control my-2 mb-2">
<label for="">End Date</label>
<input type="date" name="date_of_registration[end]" id="" class="form-control my-2 mb-2">
<hr/>
<button type="submit" class="btn btn-success my-2">Filter</button>
<button type="reset" class="btn btn-secondary my-2">Reset</button>
</form>
CONTROLLER FUNCTION
$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);
I found the answer. I solved this problem using Pipeline Design Pattern
$query = tbl_company::query();
if ($req->has('state')) {
$query->whereIn('state', $req->input('state'));
}
if ($req->has('district')) {
$query->whereIn('district', $req->input('district'));
}
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]
);
}
if ($req['authorized_capital']['start'] && $req['authorized_capital']['end']) {
$query->whereBetween(
"authorized_capital",
[$req['authorized_capital']['start'], $req['authorized_capital']['end']]
);
}
// filtered data.
$data = $query->paginate(100);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.