我的分页有问题 . 在第一页中,分页链接显示完美,但在下一页中则没有 .
业务逻辑:在ListsController上显示方法,显示该列表的一组订阅者,对结果进行分页 .
控制器:
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);
}
知识库
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;
}
视图:
<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() }}
在第一页中工作得很好,但分页链接在其他页面中消失了......
例如:
domain.com/lists/10 //分页确定domain.com/lists/10?page=1 //分页OK域名/lists/10?page=2 //分页消失
:(
任何线索?
解:
嗯...我的错误是在我的Repository类的这一行:
原版的:
$result->totalItems = $query->count();
固定:
$result->totalItems = $this->subscriber->where('list_id', $list_id)->count();
现在正在努力 . 感谢@ sam-sullivan关于转储变量的评论 .
1 回答
这就是问题所在:
在我的存储库类中:
但是,正确的方法是:
我必须在我的订阅者模型对象中应用where子句,然后计算结果...由于某些原因,在查询对象中使用count方法不能以相同的方式工作 . 现在在我请求的每个页面中,totalItems参数都填充了数据 .