1
votes

I have a issue with my pagination. In the first page, the pagination links shown perfectly, but in next pages don't.

Business Logic: show method on ListsController, shows a group of subscribers for that list, paginating the results.

Controller:

    public function show($id)
    {
        $page = Input::get('page', 1);

        $perPage = 5;

        $pagiData = $this->subscriber->byList($id, $page, $perPage);

        $subscribers = Paginator::make($pagiData->items, $pagiData->totalItems, $perPage);

        return View::make('subscribers.index', compact('subscribers'))->with('list_id', $id);
    }

Repository

    public function byList($list_id, $page = 1, $limit = 10)
{
    $result = new \StdClass;
    $result->page = $page;
    $result->limit = $limit;
    $result->totalItems = 0;
    $result->items = array();

    $query = $this->subscriber->where('list_id', $list_id)
        ->orderBy('created_at', 'desc');

    $subscribers = $query->skip( $limit * ($page - 1) )
        ->take($limit)
        ->get();

    $result->totalItems = $query->count();
    $result->items = $subscribers->all();

    return $result;
}

View:

    <table id="main">
    <thead>
        <tr>
            <th>E-mail</th>
            <th>Subscrito el:</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
    @foreach($subscribers as $item)
        <tr>
            <td> {{ $item->email }}</td>
            <td>{{ $item->created_at }} </td>
            <td>
                {{ Form::open(['url' => 'subscribers/'. $item->id, 'method' => 'get']) }}
                <button>
                    Editar
                </button>
                {{ Form::close() }}
            </td>
        </tr>
    @endforeach
    </tbody>
</table>

{{ $subscribers->links() }}

Works fine in the first page, but the pagination links disappear in the others...

Ex:

domain.com/lists/10 // Pagination OK

domain.com/lists/10?page=1 // Pagination OK

domain.com/lists/10?page=2 // Pagination goes away

:(

Any clue?


SOLUTION:

Well... my error was in this line, on my Repository class:

Original:

$result->totalItems = $query->count();

Fixed:

$result->totalItems = $this->subscriber->where('list_id', $list_id)->count();

Now is working. Thank you @sam-sullivan for your comment about dump the variable.

1
Do you have enough results from the DB to fill that many pages?ceejayoz
Of course @ceejayoz, in fact the first page show the pagination because exist more items for show... but in the second page, the items are shown but the pagination is missing!!jfreites
I'm a little stumped, but if you could throw a var_dump($pagiData); in your controller and update the question with the output from ?page=2 that could help..Sam
Well, the variable $result->totalItems is NULL when page 2 is requested. Weird, I supposed that is my issue.jfreites

1 Answers

1
votes

This was the problem:

In my repository class:

$result->totalItems = $query->count();
$result->items = $subscribers->all();

But, the correct way was:

$result->totalItems = $this->subscriber->where('list_id', $list_id)->count();;
$result->items = $subscribers->all();

I must apply the where clause in my subscriber model object and later count results... use the count method in the query object for some reasons dont work in the same way. Now in every page I request the totalItems parameter is filled with data.