Pagination is not working ?
@foreach ($category->products as $_product)
{{-- {{ $_product->name }} --}}
{{ $_product->name }}
@if ($_product->special_price)
<h5>{{ $_product->special_price }}</h5>
<h6 class="text-muted ml-2"><del>{{ $_product->price }}</del></h6>
@else
<h5>{{ $_product->price }}</h5>
@endif
</div>
<div class="d-flex align-items-center justify-content-center mb-1">
<small class="fa fa-star text-primary mr-1"></small>
<small class="fa fa-star text-primary mr-1"></small>
<small class="fa fa-star text-primary mr-1"></small>
<small class="fa fa-star text-primary mr-1"></small>
<small class="fa fa-star text-primary mr-1"></small>
<small>(99)</small>
</div>
</div>
</div>
</div>
@endforeach
{{ $category->products->links() }}
---------- controller -----------------
public function categoryData(Request $request, $url_key)
{
$category = Category::with(['products' => function($query) use($request){
if($request->has('sorting') && $request->sorting == 'latest'){
$query->orderBy('id', 'desc')->paginate(3);
} elseif($request->has('sorting') && $request->sorting == 'low_to_high') {
$query->orderBy('price', 'asc')->paginate(3);
} elseif($request->has('sorting') && $request->sorting == 'high_to_low') {
$query->orderBy('price', 'desc')->paginate(3);
}
}])->where('url_key', $url_key)->first();
if ($category) {
return view('admin.category.category', compact('category'));
} else {
abort(403);
}
}
Try to use this code, paginate after category products are fetched
$category = Category::with(['products' => function($query) use($request) {
if($request->has('sorting') && $request->sorting == 'latest'){
$query->orderBy('id', 'desc');
} elseif($request->has('sorting') && $request->sorting == 'low_to_high') {
$query->orderBy('price', 'asc');
} elseif($request->has('sorting') && $request->sorting == 'high_to_low') {
$query->orderBy('price', 'desc');
}
}])->where('url_key', $url_key)->first();
if ($category) {
$category->products = $category->products->paginate(3);
return view('admin.category.category', compact('category'));
} else {
abort(403);
}
}
@gych
it is not working.
I have error :
Method Illuminate\Database\Eloquent\Collection::paginate does not exist.
@rohitjangir Ah mb it doesn't work directly on collections
Try this code, I've tested it first now and this should work
$category = Category::where('url_key', $url_key)->first();
if ($category) {
$productsQuery = $category->products();
if ($request->has('sorting')) {
if ($request->sorting == 'latest') {
$productsQuery->orderBy('id', 'desc');
} elseif ($request->sorting == 'low_to_high') {
$productsQuery->orderBy('price', 'asc');
} elseif ($request->sorting == 'high_to_low') {
$productsQuery->orderBy('price', 'desc');
}
}
$category->products = $productsQuery->paginate(3);
return view('admin.category.category', compact('category'));
} else {
abort(403);
}
This is the basis of doing a pagination query on a relationship.
$products = $category->products()->paginate(3);
For all your conditional statements, I suggest using when methods on your query. E.g.
->when($request->sorting == 'latest', fn($q) => $q->orderBy('id', 'desc'))
Please or to participate in this conversation.