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

copain's avatar

Date search in Laravel 5.5

i have a table with 3 search functions, [1.search box, 2.drop-down 3. date-picker] now i'm having problem with (date-picker) how do i make it work inside my controller i want to have like Date_From and Date_To so i can filter the date, but in my DB i only have the default timestamp which is the created_at column is it possible to use it? and how?

Note: Search box and drop down search is working fine with this code, all i need is the code for date search and where to put it inside my controller and blade.

CONTROLLER:


 public function index(Request $request)
    {
     
        $search=$request->input('search'); 
      

        if(request()->has('lead_status')){
            $leads=Lead::where('lead_status', request('lead_status'))
            ->paginate(5)
            ->appends('lead_status',request('lead_status'));
        }
    
        else{

            $leads=Lead::orderBy('created_at','desc')->search($search)->paginate(5);
      
        }

        return  view ('leads.index')->with('leads',$leads);
    
    }



View.Blade


<form action="{{route('leads.index')}}" method="get" class="form-inline" >
 <!--Start Search Box -->
    <div class="form-group">

    <input type="text" name="search" class="form-controll" placeholder="Enter Name" value=" 
    {{isset($search) ? $search : ''}}" style="text-transform:uppercase;" >

    </div>

    <div class="form-group">

    <button type="submit"><i class="fa fa-search"></i></button>

    </div>
    <!--End Search Box -->

     <!--Start Drop Down Filter -->

    <div class="form-group">
     <div class="dropdown">
        <button class="dropbtn">Select Status <i class="fa fa-caret-down"></i></button>
          <div class="dropdown-content">
          <a href="/leads?lead_status=enrolled">ENROLLED</a>
          <a href="/leads?lead_status=pending">PENDING</a>
          <a href="/leads?lead_status=cancel">CANCEL</a>
           
      </div>
   </div>

    <!--End Drop Down Filter -->

  <!--Start Date Picker-->


<div class="form-group">
   <label>FROM</label>
   <input type="date"  name="from" value="" class="form-control" >

    <label>TO</label>
   <input type="date"  name="to" value="" class="form-control" >

   <div class="form-group">
    <button type="submit"><i class="fa fa-search"></i></button>
    </div>                   
   </div>

  <!--End Date Picker-->



    </form>
0 likes
5 replies
bobbybouwmann's avatar

Well I'm going to leave creating the date picker for yourself. This is basic html and javascript!

So you first need to find out what format the datepicker return the date. If you know this you can continue with your controller.

You can easily setup dates using Carbon. When you have Carbon instances you can just do your queries!

public function index(Request $request)
{
    // Check if both fields are filled
    if ($request->has('from') && $request->has('to')) {
        $from = \Carbon\Carbon::parseFromFormat('Y-m-d', $request->get('from'));
        $to = \Carbon\Carbon::parseFromFormat('Y-m-d', $request->get('from'));

        $leads = Lead::whereBetween('created_at', [$from, $to])->paginate(5);
    }
}

This should give you the basics for your needs ;)

Documentation: https://laravel.com/docs/5.6/queries#where-clauses

1 like
copain's avatar

@bobbybouwmann i used your'e suggested code it only works if i remove the 'Y-m-d' but i ran into another problem if i search (From May 1 To May31) April also come out in the table haha

bobbybouwmann's avatar

Should be timezone related! You can update any date time object to any timezone if you use with Carbon ;)

copain's avatar

@bobbybouwmann @Cronix if i use the parseFromFormat it Doesn't work its showing timezone problem. But when i use Parse and removed the 'y-d-m' it worked but when i run search again in may, april will show on the last page haha but when i search other month it works no issue.. mybe my may data is a bit problem haha but anyways thanks man ill just have to delete my may data and see if it works hehe only May data having issue the rest are okay.. hehe

Please or to participate in this conversation.