How to pass query string with pagination in laravel

Created at 30-Aug-2021 , By samar

How to pass query string with pagination in laravel

Hello everyone, in this post we will look at how to solve "How to pass query string with pagination in laravel" in programming.

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
  • 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() }}
    

    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

Related Queries

Back to code snippet queries related laravel

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.