Hello guys,
I have 2 tables categories and products. In category.blade.php I would like to paginate products. In my CategoryController I have:
$categories = Category::where('visible',1)->orderBy('order', 'asc')->with(['products'=>function($q) use ($id) {
$q->where('category_id', $id)->where('visible', 1)->orderBy('order', 'asc')->paginate(8);
}])->get();
in view I have:
@foreach($categories as $category)
@foreach($category->products as $product)
@php
$productsPaginator = $product->paginate(8);
@endphp
<div class="col-sm-6 col-md-4 col-lg-3 ftco-animate">
<div class="product">
<h3><a href="#">{{ $product->title }}</a></h3>
</div>
</div>
@endforeach
@endforeach
{{ $productsPaginator->links() }}
In DB are 56 products, but each of them have other category_id. It works, but:
{{ $productsPaginator->links() }}
display to me 7 tabs from 1 to 7 - each tabs 8 products. Not quite correctly, it should display as many tabs as there are products in a given category, not as many as there are all. For example, in the category I have 20 products, it should display 3 tabs, not 7. At the moment an error pops up after clicking 4 tabs.
Can someone help? So much thanks.