thebadspace/app/Services/PaginationService.php

44 lines
1 KiB
PHP
Raw Permalink Normal View History

<?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];
}
}