Skip to content

Commit

Permalink
Merge pull request #14 from one2tek/1.0
Browse files Browse the repository at this point in the history
Added method getWithCount.
  • Loading branch information
gentritabazi authored Oct 4, 2020
2 parents 3daa7a6 + 2ba8bdd commit 7fecc67
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Database/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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();
}
}

0 comments on commit 7fecc67

Please sign in to comment.