mirror of
https://koodu.h-i.works/projects/thebadspace
synced 2025-05-06 14:41:02 -05:00
Pagination is going to need some additional features in the near future, so it made sense to give it it's own seperate class.
44 lines
1.0 KiB
PHP
44 lines
1.0 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];
|
|
}
|
|
}
|