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

noblemfd's avatar

Laravel Search Between $from and $to date not working

In my Laravel-5.8, I have this code:

Controller:

    if ($request->isMethod('post'))
    {
        $from = $request->input('from');
        $to   = $request->input('to');
        if ($request->has('search'))
        {
            // select search
            $searchReports = DB::table('hr_leave_requests AS lr')
                    ->join('hr_leave_types AS lt', 'lr.leave_type_id', '=', 'lt.id')
                    ->join('hr_employees AS em', 'lr.employee_id', '=', 'em.id')
                    ->where('lr.leave_status', 4)
                    ->where('lr.company_id', $userCompany)
                    ->whereBetween('lr.commencement_date', [$from, $to])
                    ->select('em.employee_code',DB::raw("CONCAT(em.first_name,' ',em.last_name) as full_name"),'lr.commencement_date','lr.resumption_date','lr.no_of_days','lt.leave_type_name')
                    ->get();
            return view('report.leave_reports.hr_leave_taken_by_date_report',['searchReports' => $searchReports]);
        }

This is the view blade:

< script type = "text/javascript" >
  //        $(document).ready(function() {
  $(function() {
    $('.fromDate').datepicker({
      dateFormat: 'dd-mm-yy',
      changeMonth: true,
      changeYear: true,
      showAnim: 'slideDown',
      duration: 'fast',
      yearRange: new Date().getFullYear() + ':' + new Date().getFullYear(),

    });

  }); <
/script>
<form action="{{route('reports.leaveReport')}}" method="POST" class="form-horizontal" enctype="multipart/form-data">
  @csrf
  <div class="container">
    <div class="row">
      <label for="from" class="col-form-label">From:</label>
      <div class="col-md-3">
        <div class="input-group">
          <div class="input-group-prepend">
            <span class="input-group-text"><i class="far fa-calendar-alt"></i></span>
          </div>
          <input type="text" placeholder="dd/mm/yyyy" readonly autocomplete="off" name="from" class="form-control fromDate">
        </div>
      </div>
      <label for="from" class="col-form-label">To:</label>
      <div class="col-md-3">
        <div class="input-group">
          <div class="input-group-prepend">
            <span class="input-group-text"><i class="far fa-calendar-alt"></i></span>
          </div>
          <input type="text" placeholder="dd/mm/yyyy" readonly autocomplete="off" name="to" class="form-control fromDate">
        </div>
      </div>

      <div class="col-md-4">
        <button type="submit" class="btn btn-primary btn-sm" name="search">Search</button>

      </div>
    </div>
  </div>
</form>

Route:

    Route::get('reports/leaveReport', 'LeaveReportsController@leaveReport')->name('reports.leaveReport');
    Route::post('reports/leaveReport', 'LeaveReportsController@leaveReport')->name('reports.leave');

I am using JQuery-UI datepicker.

I am using commencement_date for $from and $to. As I select #from and $to and submit, I expect it to filter according to the selected date, but nothing is displayed.

If I remove the $from and $to and load it directly on the view blade, it works fine.

How do I resolve this?

Thanks

0 likes
4 replies
tykus's avatar

What is the format of the dates assigned to $from and $to variables? If not in the format yyyy-mm-dd, then convert to that format, e.g. if the date in the Request is of the form dd-mm-yy, then create a Carbon instance based on that received format:

$from = Carbon::createFromFormat('dd-mm-yy', $request->input('from'));
$to = Carbon::createFromFormat('dd-mm-yy', $request->input('to'));
noblemfd's avatar

@tykus - When I used:

 $from = Carbon::createFromFormat('dd-mm-yy', $request->input('from'));
 $to = Carbon::createFromFormat('dd-mm-yy', $request->input('to'));

I got this error:

#0 C:\xampp\htdocs\myapp\vendor\esbot\carbon\src\Carbon\Traits\Creator.php(645): Carbon\Carbon::rawCreateFromFormat('dd-mm-yy', '01-03-2021', NULL)
#1 C:\xampp\htdocs\myapp\app\Http\Controllers\Report\LeaveReportsController.php(136): Carbon\Carbon::createFromFormat('dd-mm-yy', '01-03-2021')

In the database the date appears this way: 2021-03-15

noblemfd's avatar

@snapey -It works when submitted at the first instance, but if I do the search again or any other one, it throws an error:

#0 C:\xampp\htdocs\myapp\vendor\

esbot\carbon\src\Carbon\Traits\Creator.php(645): Carbon\Carbon::rawCreateFromFormat('d-m-Y', NULL, NULL) #1 C:\xampp\htdocs\myapp\app\Http\Controllers\Report\LeaveReportsController.php(135): Carbon\Carbon::createFromFormat('d-m-Y', NULL)

and this is on the same line:

$from = Carbon::createFromFormat('d-m-Y', $request->input('from'));

Please or to participate in this conversation.