I’m attempting to implement pagination in Laravel for a specific category, but I’m facing issues with adjusting the pagination offset correctly based on user input. Below is a snippet of my controller method where I calculate the pagination offset, but it doesn’t seem to work as expected:
// Snippet from ArchiveController.php
$productsOnPage = 12;
$category = Category::where('category', '=', $productType)->first();
$productForCount = Product::all()->where('category_id', $category->id);
$productCount = $productForCount->count();
$maxPage = ceil($productCount / $productsOnPage);
// Pagination offset calculation
...
I’ve tried to calculate the pagination offset based on the user input for navigating between pages. However, even though I’ve calculated the maximum number of pages and adjusted the offset accordingly, the pagination doesn’t seem to work properly.
New contributor