Skip to content

Group By

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

This method agroups rows to summarize.

Syntax

public function groupBy(string $column1, string $column2, ...): self;
public function groupBy(array $columns): self;
public function groupBy(string $sql): self;

Example 1

PHP's way:

$repository->groupBy('user_id', 1);

SQL's Representation:

GROUP BY
  user_id, 1

Example 2

PHP's way:

$repository->groupBy([
    'brand_id',
    'category_id',
]);

SQL's Representation:

GROUP BY
  brand_id, category_id

Example 3

PHP's way:

$repository->groupBy('customer_id, CAST(created_at AS DATE)');

SQL's Representation:

GROUP BY
  customer_id, CAST(created_at AS DATE)

🡄 Grouped Conditions | Having 🡆