diff --git a/phpstan.neon.dist b/phpstan.neon.dist index be56dae6..bc9ae99d 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -4,3 +4,5 @@ parameters: level: 0 paths: - %currentWorkingDirectory%/src/ + excludes_analyse: + - %currentWorkingDirectory%/src/macros/* diff --git a/src/BakeryServiceProvider.php b/src/BakeryServiceProvider.php index 0c80a819..5a3a989b 100644 --- a/src/BakeryServiceProvider.php +++ b/src/BakeryServiceProvider.php @@ -60,6 +60,8 @@ public function register() $this->registerBakery(); + $this->registerMacros(); + $this->commands([ Console\InstallCommand::class, Console\ModelSchemaCommand::class, @@ -93,4 +95,14 @@ protected function registerSecurityRules() DocumentValidator::addRule(new DisableIntrospection()); } } + + /** + * Register the macros used by Bakery. + * + * @return void + */ + protected function registerMacros() + { + require_once __DIR__.'/macros/bakeryPaginate.php'; // TODO: Remove this once fixed upstream. + } } diff --git a/src/Queries/EloquentCollectionQuery.php b/src/Queries/EloquentCollectionQuery.php index 7652a3cd..510472a9 100644 --- a/src/Queries/EloquentCollectionQuery.php +++ b/src/Queries/EloquentCollectionQuery.php @@ -114,7 +114,6 @@ public function resolve(Arguments $args, $root, $context, ResolveInfo $info): Le $query = $this->applyOrderBy($query, $args->orderBy); } - return $query->distinct($this->model->getQualifiedKeyName()) - ->paginate($count, ['*'], 'page', $page); + return $query->distinct()->bakeryPaginate($count, ['*'], 'page', $page); } } diff --git a/src/macros/bakeryPaginate.php b/src/macros/bakeryPaginate.php new file mode 100644 index 00000000..f4333116 --- /dev/null +++ b/src/macros/bakeryPaginate.php @@ -0,0 +1,32 @@ +model->getPerPage(); + + $countColumns = $this->query->distinct ? [$this->model->getQualifiedKeyName()] : ['*']; + + $results = ($total = $this->toBase()->getCountForPagination($countColumns)) + ? $this->forPage($page, $perPage)->get($columns) + : $this->model->newCollection(); + + return $this->paginator($results, $total, $perPage, $page, [ + 'path' => Paginator::resolveCurrentPath(), + 'pageName' => $pageName, + ]); +});