【问题标题】:Laravel 4 Pagination links not shown on the following pagesLaravel 4 分页链接未显示在以下页面上
【发布时间】: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-&gt;totalItems 为 NULL。奇怪,我以为这是我的问题。

标签: php laravel pagination


【解决方案1】:

这就是问题所在:

在我的存储库类中:

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

但是,正确的方法是:

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

我必须在我的订阅者模型对象中应用 where 子句,然后再计算结果...在查询对象中使用 count 方法,因为某些原因不能以相同的方式工作。现在,在我请求的每个页面中,totalItems 参数都填充了数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-27
    • 2013-04-07
    • 1970-01-01
    • 2017-12-23
    • 1970-01-01
    • 2014-11-26
    • 2022-01-18
    相关资源
    最近更新 更多