How to pass query string with pagination in laravel

You can pass query string using appends() method with pagination in laravel. You can use paginate on model object and after that can use appends(['key'=>'value']) method to pass the query string to url with pagination

Answers 1
  • Pass multiple parameters as query string with pagination in laravel controller

    //routes\web.php
    use App\Http\Controllers\HomeController;
    Route::get('/pagination-with-query-string', [HomeController::class, 'getRecords']);
    
    //app\Http\Controllers\HomeController.php
    use App\Models\User;
    public function getRecords(){
        $users = User::select('*')->paginate(10);
        $users = $users->appends(['field1'=>'value1', 'field2' => 'value2']);
        return view('search')->with(['users'=>$users]);
    }
    
    //resources\views\search.blade.php
    <table>
        @foreach ($users as $user)
        <tr>
            <td> {{ $user->name }}  </td>
        </tr>
        @endforeach
    </table>
    {{ $users->links() }}
    
    0

    You can add multiple parameters as query string to URL with pagination in laravel

    Copy/Paste code and it will apped query string to url with pagination on click pagination link in laravel

    Output :

    http://localhost:8000/pagination-with-query-string?field1=value1&field2=value2&page=2

  • Back to code snippet queries related laravel

Related queries helps you to understand and may also used for implementation of current code snippet