Simplest way to reduce the page number links in Laravel
Recently and currently(Laravel 5.6) Laravel is using Bootstrap-4 and they've updated all their views accordingly, and when you want to use Laravel's pagination feature you may have a bit of a problem in the mobile world as there are too many page links to be shown in an esthetic way on a mobile screen.
So you can do a lot of things to resolve this problem but the simplest way available is to change the view which defines the links listening.
First, we need to publish that view, this will force Laravel to use the published view, not the one residing in the app-source/vendor directory.
We do that with the following command:
php artisan vendor:publish --tag=laravel-pagination
You should immediately see after executing the command the following:
Copied Directory [\vendor\laravel\framework\src\Illuminate\Pagination\resources\views] To [\resources\views\vendor\pagination] Publishing complete.
We can see that in the file(bootstrap-4.blade) which is the default file there is a check for the separator(three dots), so we want to reduce the number of links before the separator and after the separator, we will check if we have got to the separator and if not we assume that we are at the beginning of the array, later when we found the separator we change the key to match the next element after the separator.
php artisan vendor:publish --tag=laravel-pagination
Now after that let's actually reduce the number of links. We will do that by skipping the rest of the links after a certain amount before and after a separator:
php artisan vendor:publish --tag=laravel-pagination
The number 3 allows a maximum of 4 number + 1 special number(last/first) is a considerable reduction.
We can optionally add the class 'justify-content-center' to center our 'ul' element.
And here is the full code of the view in case of a quick copy paste replacement it will work with Laravel 5.6.
php artisan vendor:publish --tag=laravel-pagination