You use timestamps as dates or DATE style ex. 01/01/2017 ?
if ($request->has('from_date')) {
$user = $user->where('date_of_visit', '>=', $fromDate);
}
if ($request->has('to_date')) {
$user = $user->where('date_of_visit', '<=', $toDate);
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
In my case, I have to filter records by using from-date and to-date , I have tired with where between But it works only if I enter both from-date and to-date
public function searchCustomers(Request $request, CustomerProfile $user)
{
$fromDate = $request->get('from_date');
$toDate = $request->get('to_date');
$user = $user->newQuery();
if ($request->has('city')) {
$user->where('city', $request->input('city'));
}
if ($request->has('from_date') && $request->has('to_date')) {
$user->whereBetween('date_of_visit', [$fromDate, $toDate]);
}
$results = $user->get();
return response()->json($results);
}
But sometimes I just want to search with only from-date, and sometimes I want to search with only to-date, and sometimes I want to search with both from date and to-date,
How can I get above output??
Please or to participate in this conversation.