groupBy

Purpose

The groupBy method allows you to group records based on specified criteria and apply aggregate operations (like counting, averaging, summing, etc.) to each group. This method is particularly useful for generating reports, statistics, and summaries, enabling you to analyze data efficiently.

Example Usage

use Lib\Prisma\Classes\Prisma;
    
  $prisma = Prisma::getInstance(); 
  $users = $prisma->user->groupBy([
      'by' => 'country', // Group by a single field 'country' or multiple fields ['country', 'city']
  ]);
  
  echo "<pre>";
  echo "Users grouped by country: " . print_r($users, true);
  echo "</pre>";

Example Usage With Where Condition

use Lib\Prisma\Classes\Prisma;
    
  $prisma = Prisma::getInstance(); 
  $users = $prisma->user->groupBy([
      'by' => 'country', // Group by a single field 'country' or multiple fields ['country', 'city']
      'where' => [
          'email' => [
              'contains' => 'example.com'
          ]
      ]
      'orderBy' => ['name'],
      '_count' => ['email' => true],
      '_max' => ['email' => true],
      '_min' => ['email' => true],
  ]);
  
  echo "<pre>";
  echo "Users grouped by country: " . print_r($users, true);
  echo "</pre>";

Important Notes

Required Key: The by key in the $criteria array is mandatory. It specifies the field(s) by which the records should be grouped. You can group records by a single field by passing a string, or by multiple fields by passing an array of strings.