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

Manjunath's avatar

Filter record using from date and to date

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??

0 likes
3 replies
papa's avatar

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);
}
2 likes
ChristophHarms's avatar

Just check if from_date and/or to_date are filled, and if not, fill in some sensible defaults. In this case, I'm assuming that if there's only a to_date, we want to search from today until to_date, and if there's only a from_date, we want to search from from_date up to a hundred years from now (aka forever ;) ). You can change that behaviour to whatever you want.

Also, note that starting from Laravel 5.5, the Request::has() method will return true if a key is present in the input, even if it's value is null, so you should use the Request::filled() method to see if there is actually a value stored behind that key.

public function searchCustomers(Request $request, CustomerProfile $user)
 {
        $fromDate = $request->input('from_date') ?? Carbon::today();
        $toDate = $request->input('to_date') ?? Carbon::today()->addYears(100);
        
        $user = $user->newQuery();

         if ($request->filled('city')) {
             $user->where('city', $request->input('city'));
         }
        
        $user->whereBetween('date_of_visit', [$fromDate, $toDate]);

        $results = $user->get();

        return response()->json($results);
}
2 likes
hosamzewain's avatar

public function filterByDate(Request $request) { $query = ModelName::query();

// Get the 'from_date' and 'to_date' from the request
$fromDate = $request->input('from_date');
$toDate = $request->input('to_date');

// Apply 'from_date' filter if it is present
if ($fromDate) {
    $query->where('date_column', '>=', $fromDate);
}

// Apply 'to_date' filter if it is present
if ($toDate) {
    $query->where('date_column', '<=', $toDate);
}

// Execute the query and get the results
$results = $query->get();

}

Please or to participate in this conversation.