如何在laravel中显示分页? [英] How can I display pagination in laravel?

查看:289
本文介绍了如何在laravel中显示分页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用laravel 5.6

I use laravel 5.6

我尝试使用此文档: https://laravel.com/docs/5.6/pagination

我的控制器是这样的:

public function index()
{
    $products = $this->product->list(); 
    dd($products);
    return view('admin.product.index',compact('products'));
}

我的列表功能是这样的:

My list function like this :

public function scopeList($query)
{
    return $query->orderBy('updated_at', 'desc')->paginate(20);
}

如果我dd($products);,则结果如下:

在laravel视图上,添加以下内容:

On the view laravel, I add this :

 {{ $products->links() }}

然后我在网上检查,结果为空

Then I check on the web, the result empty

为什么结果为空?

有什么问题吗?

如果未显示图片,请在此处访问: https://postimg.org/image/w39usbfrv/

If the image not display, access here : https://postimg.org/image/w39usbfrv/

推荐答案

您的控制器代码将如下所示

your controller code will look like this

$products = DB::table('products')->paginate(20);
return view('admin.product.index', ['products' => $products ]);

您的查看页面代码如下

   @foreach ($products as $product)
         {{ $product->product_name}}
    @endforeach
    {{ $products->links() }}

注意:添加的项目超出您的限制,以查看分页链接

Note : Add more items more than your limit to view pagination links

这篇关于如何在laravel中显示分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆