【问题标题】:Laravel 5 Paginator - Item Ranges missing (Showing x of x )Laravel 5 Paginator - 缺少项目范围(显示 x of x )
【发布时间】:2015-03-12 21:55:36
【问题描述】:

我从 Laravel 4 升级到 Laravel 5,并注意到我不能再在 Paginator 对象上调用 getFrom()getTo()

我可以在源代码 (Illuminate\Pagination\Pagintor.php) 中看到,与 L4 相比,它不再具有 protected function calculateItemRanges()。我在这里错过了什么吗?如何显示范围,例如Now showing x of x现在在 laravel 5 中?这是我现在必须自己添加的东西吗?为什么一开始就被删除了?

【问题讨论】:

    标签: php laravel pagination laravel-5


    【解决方案1】:

    考虑到这些函数的当前逻辑,我会说 firstItem() 和 lastItem() 可能会有问题如果没有检索到数据

    例如控制器中的代码:

    $users = App\User::select('id')->paginate(10);
    $begin =  $users->firstItem();
    $end = $users->lastItem();
    
    return view('users/index')->with('begin',$begin)->with('end',$end);
    

    视图中的代码:

    <p>showing item {{$begin}} to {{$end}}</p>
    

    结果显示错误信息:显示项目 1 到 0。

    我创建了一个issue 并就此事提出了 laravel/framework 的更改。

    【讨论】:

    • 很好,Carter,在本地处理这个问题,希望修复能在框架中实现
    【解决方案2】:

    这方面的新方法称为firstItem()lastItem()

    In the source:

    /**
     * Get the number of the first item in the slice.
     *
     * @return int
     */
    public function firstItem()
    {
        return ($this->currentPage - 1) * $this->perPage + 1;
    }
    
    /**
     * Get the number of the last item in the slice.
     *
     * @return int
     */
    public function lastItem()
    {
        return $this->firstItem() + $this->count() - 1;
    }
    

    【讨论】:

    • 非常感谢。奇怪的是,文档中没有提到这一点 [laravel.com/docs/5.0/pagination] 不再
    • 这两个函数()可能不正确,请看下面我的回答。
    猜你喜欢
    • 1970-01-01
    • 2013-11-01
    • 2023-01-04
    • 2018-08-29
    • 2023-03-16
    • 2020-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多