Skip to content

Commit 6ed639b

Browse files
committed
refactor: Enhance pagination methods in CustomBuilder and improve code formatting in EloquentDatabase
1 parent 1968ced commit 6ed639b

File tree

2 files changed

+62
-14
lines changed

2 files changed

+62
-14
lines changed

src/Database/CustomBuilder.php

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Illuminate\Database\Eloquent\Builder;
66
use Illuminate\Pagination\LengthAwarePaginator;
7+
use Illuminate\Pagination\Paginator;
8+
use Illuminate\Pagination\CursorPaginator;
79

810
class CustomBuilder extends Builder
911
{
@@ -16,12 +18,58 @@ class CustomBuilder extends Builder
1618
public function paginateWithQueryString($perPage = 15)
1719
{
1820
$paginator = $this->paginate($perPage);
21+
$this->appendQueryString($paginator);
22+
23+
return $paginator;
24+
}
25+
26+
/**
27+
* Simple paginate the given query and append query string parameters.
28+
*
29+
* @param int $perPage
30+
* @return Paginator
31+
*/
32+
public function simplePaginateWithQueryString($perPage = 15)
33+
{
34+
$paginator = $this->simplePaginate($perPage);
35+
$this->appendQueryString($paginator);
36+
37+
return $paginator;
38+
}
39+
40+
/**
41+
* Cursor paginate the given query and append query string parameters.
42+
*
43+
* @param int $perPage
44+
* @param array $columns
45+
* @param string $cursorName
46+
* @param string|null $cursor
47+
* @return CursorPaginator
48+
*/
49+
public function cursorPaginateWithQueryString($perPage = 15, $columns = ['*'], $cursorName = 'cursor', $cursor = null)
50+
{
51+
$paginator = $this->cursorPaginate($perPage, $columns, $cursorName, $cursor);
52+
$this->appendQueryString($paginator);
53+
54+
return $paginator;
55+
}
56+
57+
/**
58+
* Append query string parameters to the paginator.
59+
*
60+
* @param LengthAwarePaginator|Paginator|CursorPaginator $paginator
61+
* @return void
62+
*/
63+
protected function appendQueryString($paginator)
64+
{
1965
$request = \Config\Services::request();
2066
$paginator->setPath(base_url($request->getPath()));
67+
2168
$queryParams = $request->getGet();
69+
2270
unset($queryParams['page']);
71+
unset($queryParams['cursor']);
72+
2373
$paginator->appends($queryParams);
24-
25-
return $paginator;
2674
}
27-
}
75+
}

src/Database/EloquentDatabase.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* Manages the setup and configuration of Laravel's Eloquent ORM in a CodeIgniter 4 app.
2323
* Optimized for performance with caching and lazy loading.
2424
*/
25-
final class EloquentDatabase
25+
class EloquentDatabase
2626
{
2727
protected Container $container;
2828
protected Capsule $capsule;
@@ -462,7 +462,7 @@ protected function getEloquentModels(): array
462462
}
463463

464464
// Try APCu cache first (production only)
465-
$cacheKey = 'eloquent_models_'.md5(APPPATH.filemtime(APPPATH.'Models'));
465+
$cacheKey = 'eloquent_models_' . md5(APPPATH . filemtime(APPPATH . 'Models'));
466466
if (function_exists('apcu_fetch') && ENVIRONMENT === 'production') {
467467
$cached = apcu_fetch($cacheKey);
468468
if ($cached !== false) {
@@ -485,13 +485,13 @@ protected function getEloquentModels(): array
485485
*/
486486
private function discoverModels(): array
487487
{
488-
$modelPath = APPPATH.'Models/';
488+
$modelPath = APPPATH . 'Models/';
489489
if (! is_dir($modelPath)) {
490490
return [];
491491
}
492492

493493
// Use glob for better performance than DirectoryIterator
494-
$files = glob($modelPath.'*.php');
494+
$files = glob($modelPath . '*.php');
495495
if ($files === false) {
496496
return [];
497497
}
@@ -568,19 +568,19 @@ protected function registerConfigService(): void
568568

569569
protected function registerDatabaseService(): void
570570
{
571-
$this->container->singleton('db', fn () => $this->capsule->getDatabaseManager());
571+
$this->container->singleton('db', fn() => $this->capsule->getDatabaseManager());
572572
}
573573

574574
protected function registerHashService(): void
575575
{
576-
$this->container->singleton('hash', fn ($app) => new HashManager($app));
576+
$this->container->singleton('hash', fn($app) => new HashManager($app));
577577
}
578578

579579
protected function registerPaginationRenderer(): void
580580
{
581581
$this->container->singleton(
582582
PaginationRenderer::class,
583-
fn () => new PaginationRenderer
583+
fn() => new PaginationRenderer
584584
);
585585
$this->container->alias(PaginationRenderer::class, 'paginator.renderer');
586586
}
@@ -601,7 +601,7 @@ protected function configurePagination(): void
601601

602602
$container = $this->container;
603603
Paginator::viewFactoryResolver(
604-
fn () => $container->get('paginator.renderer')
604+
fn() => $container->get('paginator.renderer')
605605
);
606606

607607
Paginator::currentPageResolver(
@@ -616,8 +616,8 @@ function ($pageName = 'page') use ($request) {
616616
}
617617
);
618618

619-
Paginator::currentPathResolver(fn () => current_url());
620-
Paginator::queryStringResolver(fn () => $uri->getQuery());
619+
Paginator::currentPathResolver(fn() => current_url());
620+
Paginator::queryStringResolver(fn() => $uri->getQuery());
621621

622622
CursorPaginator::currentCursorResolver(
623623
function ($cursorName = 'cursor') use ($request) {
@@ -679,4 +679,4 @@ public function __destruct()
679679
{
680680
unset($this->sqliteHandler);
681681
}
682-
}
682+
}

0 commit comments

Comments
 (0)