Filter record using from date and to date

Posted 1 year ago by Manjunath

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 sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.