Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

What is your code to display the count?

Last updated 2 years ago.
0

I was just using the count to debug. The code was as follows:

$sites->count();
Last updated 2 years ago.
0

Have a bash at...

$sites->getTotal()

// or

$sites->links->getTotal()

I'm not 100%, but I think the count() is giving you the total on the current page. To test this, you could drop down the amount to pagination by - ie pagination by 5 rather than 15.

Hope that should do it for you.

Ref: http://laravel.com/docs/4.2/pagination

Last updated 2 years ago.
0

Yup, tried those two. getTotal() returns the correct amount, but pagination is still broken. If I hit the URL with ?page=2, I still get the results from page 1 and the offset in the SQL query isn't updated.

Last updated 2 years ago.
0

Can you post your view and controller? Have you used a custom URL for your pagination at all?

If you haven't already... If you echo out {{ $sites->links() }} in your view then Laravel will amazingly output the links required, so you can have a beer. Better still, if you are using Bootstrap, then the styling is taken care of.

The docs refer to customising this if you are using a different UI framework or require something more custom.

Last updated 2 years ago.
0

Controller:

<?php

namespace App\Controllers;

use App\Repositories\ISiteRepository;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\View;
use Illuminate\Support\MessageBag;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class Site extends Base
{
    /** @var \App\ISiteRepository $siteRepository */
    private $siteRepository;

    /**
     * Constructor to create filters for security reasons.
     */
    public function __construct(ISiteRepository $sites)
    {
        parent::__construct();

        $this->siteRepository         = $sites;
    }

    /**
     * View all sites currently registered.
     *
     * @return \Illuminate\Http\Response
     */
    public function getList()
    {
        $sites = $this->siteRepository->paginate($this->perPage);

        $this->layout->content = View::make('sites.list')->with(
            'sites',
            $sites
        );
    }

View:

@extends('rayouts.master')

@section('meta')
    
<title>Sites</title>
<link rel="canonical" href="{{{ URL::action('App\Controllers\Site@getList') }}}">
@stop

@section('page-header__title')
Sites
@stop

@section('page-header__actions')
@if (Sentry::getUser()->hasAccess('sites.create'))
{{ HTML::linkAction('App\Controllers\Site@getCreate', 'Create', '', array('class' => 'small button')) }}
@endif
@stop

@section('page-header__navigation')
{{ $sites->links() }}
@stop

@section('content')
<div class="container site list row">
    <ul class="card-list" role="list">
        @if ($sites)
        @foreach ($sites as $site)
        <li>
            <div class="panel text-center site">
                {{ HTML::linkAction('App\Controllers\Site@getView', $site->name, $site->id) }}
            </div>
        </li>
        @endforeach
        @else
        <li class="alert alert--error text-center">
            No sites found
        </li>
        @endif
    </ul>
    {{ $sites->links() }}
</div>
@stop

No custom URLs, everything is by the book (and exactly how I've used pagination in the past).

Last updated 2 years ago.
0

All looks great there. I presume that if you click on one of the links generated, the issue persists?

What's in your routes file? That is the next place I would look. You may have a route that is intervening prior to this route that is causing the issue and laravel is getting a bit lost. Perhaps a post to the same URL that is above this get? If there is, switch the order and see if that works.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

euantorano euantorano Joined 11 Dec 2013

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.