简体   繁体   中英

Laravel date range filter

I have a form that has a dropdown to select a staff member and a date from and date to field where they can pick their required dates and it should show all the selected staff member's records in that date range but it only shows all the records for that staff member, never in the required date range. This is what I have tried

Controller:

   public function vehiclereport()
    {
        $startDate = '2021/01/01';
        $endDate = '2021/12/12';
        $energy = VehicleLog::join('vehicle', 'vehicleslog.vehicle_id', '=', 'vehicle.id')
            ->join('smsstaff', 'vehicleslog.smsstaff_key', '=', 'smsstaff.smsstaff_key')
            ->when(request()->input('smsstaff_key'), function ($query) {
                $query->where('smsstaff.smsstaff_key', request()->input('smsstaff_key'));
            })
            ->whereDate('log_dt', '>=', $startDate)
            ->whereDate('log_dt', '<=', $endDate)
            ->get();
        $cars = Vehicle::get();
        $staff = Staff::all();
        return view('admin.vehiclereport', compact('energy', 'cars', 'staff', 'startDate', 'endDate'));
    }

View:

  <form>
            <select name="smsstaff_key" id="smsstaff_key">
            <option></option>
            @foreach ($staff as $staffMember) 
                <option value="{{$staffMember->smsstaff_key}}" {{request()->input('smsstaff_key') === $staffMember->smsstaff_key ? 'selected="selected"' : ''}}>{{$staffMember->name}}</option>
                @endforeach
                <input type="date" class="form-control" name="startDate">
                <input type="date" class="form-control" name="endDate">
            </select>
            <button>Filter by selected staff member</button>
            </form>

There's a few issues here:

(a) You have your inputs inside the select, move them out of the select,

(b) you have the from/to dates in a bad format. They should look like YYYY-MM-DD ,

(c) you aren't actually initialising the inputs with those dates ie doing something like <input type="date" name="startDate" value="{{ $startDate }}" /> and

(d) You aren't reading the dates from the input you currently have them static.

Try this code instead:

   public function vehiclereport()
    {
        $startDate = request()->input('startDate', '2021-01-01');
        $endDate = request()->input('endDate','2021-12-12');
        $energy = VehicleLog::join('vehicle', 'vehicleslog.vehicle_id', '=', 'vehicle.id')
            ->join('smsstaff', 'vehicleslog.smsstaff_key', '=', 'smsstaff.smsstaff_key')
            ->when(request()->input('smsstaff_key'), function ($query) {
                $query->where('smsstaff.smsstaff_key', request()->input('smsstaff_key'));
            })
            ->whereDate('log_dt', '>=', $startDate)
            ->whereDate('log_dt', '<=', $endDate)
            ->get();
        $cars = Vehicle::get();
        $staff = Staff::all();
        return view('admin.vehiclereport', compact('energy', 'cars', 'staff', 'startDate', 'endDate'));
    }

and the view:

<form>
   <select name="smsstaff_key" id="smsstaff_key">
       <option></option>
       @foreach ($staff as $staffMember) 
          <option value="{{$staffMember->smsstaff_key}}" {{request()->input('smsstaff_key') === $staffMember->smsstaff_key ? 'selected="selected"' : ''}}>{{$staffMember->name}}</option>
       @endforeach
  </select>
  <input type="date" class="form-control" name="startDate" value="{{ $startDate }}">
  <input type="date" class="form-control" name="endDate" value="{{ $endDate }}">
       
 <button>Filter by selected staff member</button>
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM