Skip to content

Conversation

@techmahedy
Copy link
Member

@techmahedy techmahedy commented Jan 9, 2026

This PR introduces a new groupByCallback() method to the Entity ORM query builder.
The method enables grouping records using custom, application-level logic, rather than being limited to database columns.

This is especially useful when grouping depends on computed values, ranges, or domain-specific rules that cannot be expressed directly in SQL.

Example: Group Users by Age Range

$groups = User::query()
  ->groupByCallback(function ($user) {
      if ($user->age < 18) return 'minor';
      if ($user->age < 65) return 'adult';
      return 'senior';
  });

Result structure:

[
    'minor'  => Collection,
    'adult'  => Collection,
    'senior' => Collection,
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant