Laravel pagination links with query string

You can pass query string to pagination link by appends() method to display the filtered data with pagination.

Answers 2
  • Pagination link with query string in laravel view

    {{ $users->appends(['keyword' => request()->get('keyword')])->links() }}
    
    0

    You can displayed filtered records with query string using pagination. 

    On click pagination link, It will create url like http://localhost:8000/search?keyword=value&page=2 which helps you to display the filttered data with pagination.

  • Pass 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(['keyword'=>'value']);
        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 query string to URL in laravel using appends() method in controller file. 

  • Back to code snippet queries related laravel

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