How can I display pagination in laravel?

你离开我真会死。 提交于 2019-12-24 16:00:07

问题


I use laravel 5.6

I try this documentation : 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 :

On the view laravel, I add this :

 {{ $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/


回答1:


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




回答2:


In laravel pagination can be done using two method

1) Using Query Builder Results

$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.

2) Using Eloquent Results

$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




回答3:


Use paginate() function

App\Product::paginate();

or by query builder:

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



回答4:


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:1 that's why empty pagination , because there is nothing to paginate.



来源:https://stackoverflow.com/questions/48903173/how-can-i-display-pagination-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!