Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I display pagination in laravel?

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 :

enter image description here

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/

like image 613
moses toh Avatar asked Oct 18 '25 11:10

moses toh


1 Answers

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

like image 170
syed mahroof Avatar answered Oct 20 '25 00:10

syed mahroof



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!