From 2ba8bdd9afe84167b68b1cab516342d9288d3f54 Mon Sep 17 00:00:00 2001 From: Gentrit Abazi Date: Sun, 4 Oct 2020 20:43:56 +0200 Subject: [PATCH] Added method getWithCount. --- src/Database/Repository.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/Database/Repository.php b/src/Database/Repository.php index a827ae6..773242c 100644 --- a/src/Database/Repository.php +++ b/src/Database/Repository.php @@ -39,6 +39,23 @@ public function get(array $options = []) return $query->get(); } + /** + * Get all resources with count. + * + * @param array $options + * + * @return Collection + */ + public function getWithCount(array $options = []) + { + $query = $this->createBaseBuilder($options); + + $totalData = $this->countRows($query); + $allRows = $query->get(); + + return ['total_data' => $totalData, 'rows' => $allRows]; + } + /** * Get a resource by its primary key. * @@ -367,4 +384,11 @@ protected function applyWhereArray($query, array $clauses) } } } + + protected function countRows($query) + { + $totalQuery = clone $query; + + return $totalQuery->offset(0)->limit(PHP_INT_MAX)->count(); + } }