Pass query string in Laravel Pagination globally

Pass query string in Laravel Pagination globally

Posted on:May 26, 2020 at 10:00 AM

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.