【发布时间】:2014-02-26 06:19:24
【问题描述】:
我的分页有问题。在第一页,分页链接显示完美,但在下一页没有。
业务逻辑:ListsController 上的 show 方法,显示该列表的一组订阅者,对结果进行分页。
控制器:
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 // 分页OK
domain.com/lists/10?page=1 // 分页OK
domain.com/lists/10?page=2 // 分页消失
:(
有什么线索吗?
解决方案:
嗯...我的错误出现在这一行,在我的 Repository 类中:
原文:
$result->totalItems = $query->count();
固定:
$result->totalItems = $this->subscriber->where('list_id', $list_id)->count();
现在正在工作。感谢@sam-sullivan 对转储变量的评论。
【问题讨论】:
-
你有足够的数据库结果来填满那么多页面吗?
-
当然@ceejayoz,实际上第一页显示了分页,因为存在更多要显示的项目...但是在第二页中,显示了项目但缺少分页!!
-
我有点难过,但如果你可以在你的控制器中抛出一个
var_dump($pagiData);并用?page=2的输出更新问题,这可能会有所帮助.. -
好吧,当请求第 2 页时,变量
$result->totalItems为 NULL。奇怪,我以为这是我的问题。
标签: php laravel pagination