Filtering records by dropdown and date period in Laravel

Viewed 258

I am building a form in Laravel and I have a drop down that lets the user pick a staff member and it shows all the data for that staff member when the the name is picked and the search button is pressed. I have a field in my table called log_dt which is the date they will choose.

I want to include some date from to date to pickers so that they can pick 2 dates, hit the search button and it will filter in that date range. I tried to implement the date pick and use a wherebetween but I got a column not found start error.

Does anyone know how to implement this?

My controller:

   public function vehiclereport(Request $request)
    {

        $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'));
             })->where('log_dt','=',$request->log_dt)
             ->whereBetween('start', array('2021-01-01', '2025-07-11'))
             ->whereBetween('end', array('2021-01-01', '2026-07-11'))
             ->get();

          

        $cars = Vehicle::get();
        $staff = Staff::all();
        
        return view('admin.vehiclereport', compact('energy', 'cars', 'staff'));
    }

My 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
            <label>
                Pick a Date
                              <input type="date" id="start" name="start"
                              
                              min="2021-01-01" max="2050-12-31" required>
            </label>
            <label>
                Pick a Date
                              <input type="date" id="end" name="end"
                              
                              min="2021-01-01" max="2050-12-31" required>
            </label>
            </select>
            <button>Filter by selected staff member</button>
            </form>
0 Answers
Related