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.
var_dump($pagiData);
in your controller and update the question with the output from?page=2
that could help.. – Sam$result->totalItems
is NULL when page 2 is requested. Weird, I supposed that is my issue. – jfreites