Anas Kasmi
Anas Kasmi

Reputation: 43

Get query string parameters are lost when using pagination links in laravel

I have tried to use pagination with laravel and everything works fine until I navigate to another page, then the request doesn't preserve the previous query string and, as a result, the query doesn't work anymore.

This is the query in the controller:

$inputDate =$request->input('date') ;
$endDate = date('Y-m-d', strtotime($inputDate. ' + 7 days'));

$shifts = DB::table('driver_shifts')
    ->join('drivers','drivers.PermitNumber','=','driver_shifts.driver_badge_id')
    ->where ('driver_shifts.shift_start_time','>',$inputDate)
    ->where ('driver_shifts.shift_start_time','<',$endDate)
    ->orderby("vehicle_id")
    ->orderby("shift_start_time")
    ->paginate(20);

return view('reports.WeeklyTaxiShifts.WeeklyTaxiShifts', compact('shifts','inputDate'));

Upvotes: 0

Views: 642

Answers (1)

mdexp
mdexp

Reputation: 3567

You can try to use:

$shifts = DB::table('driver_shifts')
    ->join('drivers','drivers.PermitNumber','=','driver_shifts.driver_badge_id')
    ->where ('driver_shifts.shift_start_time','>',$inputDate)
    ->where ('driver_shifts.shift_start_time','<',$endDate)
    ->orderby("vehicle_id")
    ->orderby("shift_start_time")
    ->paginate(20)
    // this line will put back the query string parameters on the generated links
    ->appends(request()->query());

That is also explained with a blade usage example in the documentation

Upvotes: 2

Related Questions