Laravel 4 Binding paginated

I am trying to display a list of products with matching categories using the following in my controller:

return View::make('products.index')
->with('categories', Categories::with('products')->orderBy('name', 'asc')->Paginate(15))

      

But pagination doesn't work, it displays all products (500+), while I only want to show 15 on each page. Here are my models:

Categories:

public function products()
{
    return $this->belongsToMany('Products');
}

      

Products:

public function categories()
{
    return $this->belongsToMany('Categories');
}

      

How can I make pagination work?

+3


source to share


1 answer


Try the following:

$categories = Categories::with(array('products' => function($query)
{
   $query->orderBy('name', 'asc');

 }))->paginate(15);

 return View::make('products.index', compact('categories'));

      



Hope this helps you. Here's a link to actively download.

+5


source







All Articles