Pass query string in Laravel Pagination globally

3 years ago
345 views

If you often need to pass query string in laravel imagination, I believe passing query string globally will be the best choice instead of passing in every method. Here is how you can pass query string globally.

Go to AppServiceProvider and add the following code in boot() method.


$this->app->resolving(LengthAwarePaginator::class, static function (LengthAwarePaginator $paginator) {
    return $paginator->appends(request()->query());
});
$this->app->resolving(Paginator::class, static function (Paginator $paginator) {
    return $paginator->appends(request()->query());
});

And of course, on top of your class, you need to import these files.

use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;

Thanks.

Thouhedu Islam Suchi

Thouhedul Islam

Laravel Developer

Hi, I am a Laravel Developer with a passion for web development. I specialize in PHP, particularly Laravel, as well as Javascript, MySql, and other cutting-edge web technologies.

During the day, I work for GLEIF, where I apply my skills and experience to develop innovative web applications. At night, I enjoy contributing to the community and solving problems. You can often find me on Laracasts, where I love to stay active and keep up-to-date with the latest trends in web development.

I am excited about the potential of technology and its power to transform people's lives.