Reputation: 76
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.
Upvotes: 1
Views: 810
Reputation: 76
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.
Upvotes: 1