首页 文章

Laravel - 强制分页链接显示即使只有一个页面

提问于
浏览
1

我正在使用laravel pagination,但我希望显示分页链接,无论是否只有一个页面或多个页面 .

目前,只有在有多页结果时才会显示 .

雄辩的电话

$products = Product::where('username', Sentry::getUser()->username)->paginate(25);

然后使用在视图中显示

{!! $products->links() !!}

当只有一个页面时,如何强制Laravel显示它?

2 回答

  • 2

    没有简单的方法,因为它是硬编码的 . 但是,您可以扩展SimpleBootstrapThreePresenter并覆盖hasPages()方法:

    public function hasPages()
    {
        return true;
    }
    

    代替:

    public function hasPages()
    {
        return $this->paginator->hasPages() && count($this->paginator->items()) > 0;
    }
    
  • 2

    Alexey Mezenin's answer之后,我扩展了 BootstrapThreePresenter 类:

    <?php namespace App\Extend;
    
    use Illuminate\Pagination\BootstrapThreePresenter;
    
    class CustomPaginationLinks extends BootstrapThreePresenter {
    
        public function hasPages()
        {
            return true;
        }
    
    }
    

    然后能够在视图中呈现如下:

    {!! with(new App\Extend\CustomPaginationLinks($products))->render() !!}
    

相关问题