Skip to content

Aggregation

Gilberto Junior edited this page Nov 29, 2019 · 4 revisions

Methods

Count

Count the number of rows.

Syntax

public function count(): int;

Example

$numRows = $repository
    ->where('active = 1')
    ->count();

echo $numRows . ' rows found';

Sum

Sum the field's value.

Syntax

public function sum(string $column): mixed

Example

$total = $repository
     ->where('category_id = 2')
     ->sum('price');

echo 'Total: ' . $total; 

Maximun

Get the maximun value from field.

Syntax

public function max(string $column): mixed

Example

$maximun = $repository
     ->where('paid_at IS NULL')
     ->max('canceled_at');
echo 'Max: ' . $maximun;

Minimun

Get the minimun value from field.

Syntax

public function min(string $column): mixed

Example

$minimun = $repository
     ->min('birth_date');
echo 'Min: ' . $minimun;

Average

Get the average from field.

Syntax

public function avg(string $column): mixed

Example

$average = $repository
     ->avg('age');
echo 'Average: ' . $average;

🡄 Outputs | Caching 🡆