Laravel pagination on each side not working properly

Viewed 2148

In my Laravel (5.7) controller, I have set pagination + 'on each side navigation' to 2 items. However, this is not working on my view. I'm getting much more items than 2, which causes my window to break out on small mobile devices.

What can be the cause of this and how can I fix it? I mean, I can get it fixed with CSS, but I would like to get it fixed the proper way.

My code in the controller:

public function index(Request $request)
{
    $type = 'all';

    if ($request->has('type')) {
        if ($request->type == 'all') {
            $materials = Material::paginate(10)->onEachSide(2);
        } else {
            $type = $request->type;
            $materials = Material::where('type', $type)->paginate(10)->onEachSide(2);
        }
    } else {
        $materials = Material::paginate(10)->onEachSide(2);
    }

    
    $stock = array(
        'enkelzitKajaks' => Material::where('type', 'Enkelzitkajak')->count(),
        'dubbelzitKajaks' => Material::where('type', 'Dubbelzitkajak')->count(),
        'canadeseKanos' => Material::where('type', 'Canadese kano')->count(),
        'langePeddels' => Material::where('type', 'Lange peddel')->count(),
        'kortePeddels' => Material::where('type', 'Korte peddel')->count(),
        'tonnetjes' => Material::where('type', 'Tonnetje')->count(),
        'zwemvesten' => Material::where('type', 'Zwemvest')->count()
    );

    return view('materials.index')->with('materials', $materials)->with('stock', $stock)->with('type', $type);
}

My code in the view:

<section class="pagination">
    <div class="container">
        <div class="row">
            <div class="col-12">
                {{ $materials->links() }}
            </div>
        </div>

        <hr>
    </div>
</section>

A screenshot of my view on small devices:

enter image description here

4 Answers

First of all The laravel pagination onEachSide(x) has leading and tailing parts besides the middle section. Leading two pages and tailing two pages.

You are trying with onEachSide(2) and on the 6th page. So now we try to look at the logic in pagination.

In laravel when using onEachSide(2) it will show pagination like this

leadingTwoPages currentPage - 2 currentPage currentPage +2 tailingTwoPages
1,2 4,5 6 7,8 22,23

Then think about the first part of pagination it needs to show the 1st page to the 8th page without the 3rd page. Instead of page number three, it needs to be a show button with dots.

Then instead of showing a button with dots for page number three, it will show the original 3rd-page number.

After the 6th page the laravel pagination onEachSide(2) will work fine

You can try class="pagination pagination-sm" for mobile device

It's because of a bug in Laravel 5.* versions. If you don't want to upgrade your app, you should manually change some codes in:

vendor/laravel/framework/src/illuminate/Pagination/UrlWindow.php

as mentioned and discussed in below link with Taylor Otwell. Disccus about on each side bug in Laravel 5.*

You should use onEachSide this in blade.

Remove the onEachSide from the controllers. Using onEachSide in controllers will be helpful when you are creating APIs. Instead use onEachSide in blade file.

<section class="pagination">
    <div class="container">
        <div class="row">
            <div class="col-12">
                {{ $materials->onEachSide(1)->links() }}
            </div>
        </div>

        <hr>
    </div>
</section>

Use can manage the link window by changing the parameter in onEachSide

https://laravel.com/docs/8.x/pagination#adjusting-the-pagination-link-window

Related