mirror of
https://koodu.h-i.works/projects/thebadspace
synced 2025-06-25 16:04:37 -05:00
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.
This commit is contained in:
43
app/Services/PaginationService.php
Normal file
43
app/Services/PaginationService.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?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];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user