如何在 Laravel 中显示分页?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48903173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:25:36  来源:igfitidea点击:

How can I display pagination in laravel?

phplaravelpaginationlaravel-5.6

提问by Success Man

I use laravel 5.6

我使用 Laravel 5.6

I try this documentation : https://laravel.com/docs/5.6/pagination

我试试这个文档:https: //laravel.com/docs/5.6/pagination

My controller like this :

我的控制器是这样的:

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);
}

If I dd($products);, the result like this :

如果我dd($products);,结果是这样的:

enter image description here

在此处输入图片说明

On the view laravel, I add this :

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

 {{ $products->links() }}

Then I check on the web, the result empty

然后我上网查,结果为空

Why the result empty?

为什么结果为空?

Is there something wrong?

有什么不对?

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

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

回答by syed mahroof

your controller code will look like this

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

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

your view page code look like this

您的视图页面代码如下所示

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

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

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

回答by Jazzzzzz

In laravel pagination can be done using two method

在 Laravel 中可以使用两种方法进行分页

1) Using Query Builder Results

1) 使用查询生成器结果

$products= DB::table('products')->paginate(15);

or

或者

$products= DB::table('products')->simplePaginate(15);

If you only need to display simple "Next" and "Previous" links in your pagination view, you may use the simplePaginate method.

如果您只需要在分页视图中显示简单的“Next”和“Previous”链接,您可以使用 simplePaginate 方法。

2) Using Eloquent Results

2) 使用 Eloquent 结果

$products= App\Products::paginate(15);

To print pagination links

打印分页链接

{{ $products->links() }}

customize pagination view using

使用自定义分页视图

{{ $paginator->links('view.name') }}

or you can refer this link for more custom pagination view

或者您可以参考此链接以获取更多自定义分页视图

Custom pagination view in Laravel 5

Laravel 5 中的自定义分页视图

回答by Mahdi Younesi

Use paginate()function

使用paginate()功能

App\Product::paginate();

or by query builder:

或通过查询构建器:

$products= DB::table('products')->paginate(15);

回答by user7747472

That's because, you are paginiating for 20 products and you have only 11 products. That is why pagination will only come only if there are more then the number of products you are requesting .. For you u have one page and 11 products. Look at the top #total:11, lastpage:1that's why empty pagination , because there is nothing to paginate.

那是因为,您正在为 20 个产品进行分页,而您只有 11 个产品。这就是为什么只有当您请求的产品数量多于您时才会出现分页。对于您来说,您只有一页和 11 个产品。看看顶部#total:11lastpage:1这就是空分页的原因,因为没有什么可分页的。