phayes0289's avatar

Weird Pagination Malfunction

All of my paginations in my project are suddenly not working. I don't know when it started, but I just noticed it today. I know at one time they were working. They do fully display, but none of the links work.

What could be causing that?

UPDATE 10/29/2023; Bootstrap issue?

I have figured out what the cause issue is, but have no idea how to resolve it. My site relies on Bootstrap. I had applied the bootstrap framework to the pagination service using: ''' Paginator::useBootstrap(); ''' That formats it nicely, but kills all the links.

0 likes
10 replies
LaryAI's avatar
Level 58

There could be several reasons why the pagination links are not working. Here are a few possible solutions to investigate:

  1. Check the route: Ensure that the route used for pagination is correct and matches the route defined in your application. You can verify this by checking the route file (e.g., web.php or api.php) and comparing it with the route used in the pagination links.

  2. Verify the query parameters: Pagination links typically include query parameters like page or limit. Make sure that these parameters are correctly passed in the pagination links and are being processed correctly in your controller or query.

  3. Check the controller or query: Ensure that the controller or query responsible for fetching the paginated data is correctly implemented. Verify that the pagination method or query builder is being used correctly and that the correct data is being returned.

  4. Verify the view: Check the view file responsible for rendering the pagination links. Ensure that the correct pagination method or helper function is being used to generate the links. Also, make sure that the necessary CSS and JavaScript files are included in the view to handle the pagination functionality.

  5. Check for JavaScript errors: If you are using JavaScript to handle the pagination functionality (e.g., AJAX requests), check the browser console for any JavaScript errors that might be preventing the links from working. Fixing these errors should resolve the issue.

Without more specific information about your project, it's difficult to provide a precise solution. If none of the above suggestions resolve the issue, please provide more details about your project, including the code used for pagination, and any error messages or logs you are encountering.

phayes0289's avatar

The pagination links all appear, but none of them link to anything. There are no JavaScript errors.

jlrdw's avatar

Can you show the controller code for the method and the code calling the links.

Also do some dd and look in the network tab.

phayes0289's avatar

@jlrdw

This is the code for the index controller:

  public function index(Request $request)
    {
        $searchTerm = $request->input('search');

        $users = User::orderByDesc('lastname')
            ->filter(['search' => $searchTerm]) // Pass the search term as an array
            ->paginate(20)
            ->appends(['search' => $searchTerm]); // Append the search term to the pagination links

        return view('directory.index', [
            'users' => $users,
            'search' => $searchTerm,
        ]);
    }

This is the route:

Route::get('/directory', [DirectoryController::class, 'index'])->name('directory.index');

and this is the abbreviated blade page where the output occurs:

<main id="js-page-content" role="main" class="page-content">
{{-- Search --}}
<form action="{{ route('directory.index') }}" method="get">
<div class="input-group input-group-lg mb-4 shadow-1 rounded">

    <input type="text" class="form-control shadow-inset-2" id="search" name="search" aria-label="type 2 or more letters" placeholder="Search for a name, title or rank..." value="{{ $search }}">
    <div class="input-group-append">
        <button class="btn btn-primary hidden-sm-down waves-effect waves-themed" type="submit"><i
                class="fal fa-search mr-lg-2"></i><span class="hidden-md-down">Search</span></button>
    </div>
</div>
<div class="mb-2 text-center"> [ <a href="{{ route('directory.index') }}">Clear Search</a>]</div>
</form>
{{ $users->links() }}
@foreach ($users as $user)
     //My Output Code Here
@endforeach
    {{ $users->links() }}
</main>
Snapey's avatar

how does the url compare to what you believe it should be?

Check your APP_URL in .env

phayes0289's avatar

@Snapey

There are no links. When you click on ANY of the links built by Laravel, nothing happens. This is the output:

https://app.screencast.com/dHUBf190UtNlY

This is the source code generated:

      <ul class="pagination pagination-rounded justify-content-center mt-4">

                        <li class="page-item disabled">
                            <a href="javascript:;" wire:click="previousPage" class="page-link">Prev</a>
                        </li>

                        <li class="page-item active" aria-current="page">
                            <a href="javascript:;" wire:click="gotoPage(1)" class="page-link">
                                <span>1</span>
                            </a>
                        </li>
                        <li class="page-item">
                            <a href="javascript:;" wire:click="gotoPage(2)" class="page-link">2</a>
                        </li>
                        <li class="page-item">
                            <a href="javascript:;" wire:click="gotoPage(3)" class="page-link">3</a>
                        </li>
                        <li class="page-item">
                            <a href="javascript:;" wire:click="gotoPage(4)" class="page-link">4</a>
                        </li>
                        <li class="page-item">
                            <a href="javascript:;" wire:click="gotoPage(5)" class="page-link">5</a>
                        </li>
                        <li class="page-item">
                            <a href="javascript:;" wire:click="gotoPage(6)" class="page-link">6</a>
                        </li>
                        <li class="page-item">
                            <a href="javascript:;" wire:click="gotoPage(7)" class="page-link">7</a>
                        </li>
                        <li class="page-item">
                            <a href="javascript:;" wire:click="gotoPage(8)" class="page-link">8</a>
                        </li>
                        <li class="page-item">
                            <a href="javascript:;" wire:click="gotoPage(9)" class="page-link">9</a>
                        </li>
                        <li class="page-item">
                            <a href="javascript:;" wire:click="gotoPage(10)" class="page-link">10</a>
                        </li>
                        <li class="page-item">
                            <a href="javascript:;" wire:click="gotoPage(11)" class="page-link">11</a>
                        </li>

                        <li class="page-item">
                            <a href="javascript:;" wire:click="nextPage" class="page-link">Next</a>
                        </li>
                    </ul>
phayes0289's avatar

@Snapey @jlrdw

I have figured out what the cause issue is, but have no idea how to resolve it. My site relies on Bootstrap. I had applied the bootstrap framework to the pagination service using:

Paginator::useBootstrap();

That kills all the links, but formats it nicely.

Any idea what I should do?

phayes0289's avatar

The Livewire scripts appear to load, based on Source code:

    <!-- Livewire Scripts -->

    <script src="/livewire/livewire.js?id=90730a3b0e7144480175" data-turbo-eval="false" data-turbolinks-eval="false"></script>
    <script data-turbo-eval="false" data-turbolinks-eval="false">
    if (window.livewire) {
        console.warn('Livewire: It looks like Livewire\'s @livewireScripts JavaScript assets have already been loaded. Make sure you aren\'t loading them twice.')
    }

    window.livewire = new Livewire();
    window.livewire.devTools(true);
    window.Livewire = window.livewire;
    window.livewire_app_url = '';
    window.livewire_token = 'nDjaBhw7t66yqocAlzCUnBZiAgOukwPSvW9VoxLd';

    /* Make sure Livewire loads first. */
    if (window.Alpine) {
        /* Defer showing the warning so it doesn't get buried under downstream errors. */
        document.addEventListener("DOMContentLoaded", function() {
            setTimeout(function() {
                console.warn("Livewire: It looks like AlpineJS has already been loaded. Make sure Livewire\'s scripts are loaded before Alpine.\n\n Reference docs for more info: http://laravel-livewire.com/docs/alpine-js")
            })
        });
    }

    /* Make Alpine wait until Livewire is finished rendering to do its thing. */
    window.deferLoadingAlpine = function(callback) {
        window.addEventListener('livewire:load', function() {
            callback();
        });
    };

    let started = false;

    window.addEventListener('alpine:initializing', function() {
        if (!started) {
            window.livewire.start();

            started = true;
        }
    });

    document.addEventListener("DOMContentLoaded", function() {
        if (!started) {
            window.livewire.start();

            started = true;
        }
    });
    </script>
phayes0289's avatar
phayes0289
OP
Best Answer
Level 5

RESOLVED: php artisan vendor:publish --tag=laravel-pagination

then

{{ $users->links('vendor.pagination.bootstrap-5') }}

Please or to participate in this conversation.