ForRo/app/Services/PaginationService.php
ro 682360c140 Added Pagination Service class
Pagination is going to need some additional features in the near future,
so it made sense to give it it's own seperate class.
2024-02-20 12:57:33 -06:00

44 lines
1 KiB
PHP

<?php
namespace App\Services;
use App\Models\Location;
class PaginationService
{
protected $location;
protected $limit = 15;
public function __construct(
Location $location,
) {
$this->location = $location;
}
public function getPage($pageNum)
{
$range = $pageNum * $this->limit - $this->limit;
$active = $this->location::where("active", true)->where('actions_count', '>=', 2)->get();
$locations = $this->location::where("active", true)->where('actions_count', '>=', 2)
->limit($this->limit)->offset($range)->orderBy('id', 'asc')->get();
$pageCount = ceil(count($active) / $this->limit);
$next = $pageNum + 1;
if ($next > $pageCount) {
$next = 1;
}
$prev = $pageNum - 1;
if ($prev <= 0) {
$prev = $pageCount;
}
return $result = [
'locations' => $locations,
'pageCount' => $pageCount,
'prev' => $prev,
'next' => $next];
}
}