Order By Filter

The Order By Filter allows you to sort data in either ascending or descending order, making it easier to organize and retrieve the information you need.

Order by Fields of the Current Model

Sort the current model fields in either ascending or descending order to organize your data effectively.

  use Lib\Prisma\Classes\Prisma;
    
        $prisma = Prisma::getInstance();
        $users = $prisma->user->findMany([
            'where' => [
                'name' => [
                    'contains' => 'John'
                ]
            ],
            'orderBy' => [
                'name' => 'asc' // or 'desc'
            ]
        ]);
    
        echo "<pre>";
        echo "Users found: " . print_r($users, true);
        echo "</pre>";

Order by Fields of Related Models

Sort the fields of related models in either ascending or descending order to efficiently manage and display your data relationships. When ordering by a related model's fields, the related model is included in the query, performing the necessary join operation (default is INNER JOIN). The orderBy clause specifies the sorting order.

  use Lib\Prisma\Classes\Prisma;
    
        $prisma = Prisma::getInstance();
        $users = $prisma->user->findMany([
            'where' => [
                'name' => [
                    'contains' => 'John'
                ]
            ],
            'orderBy' => [
                'posts' => [
                    'createdAt' => 'asc' // or 'desc'
                ]
            ]
        ]);
    
        echo "<pre>";
        echo "Users found: " . print_r($users, true);
        echo "</pre>";

Order by Fields of Related Models with Custom Join Types

Sort the fields of related models in either ascending or descending order to efficiently manage and display your data relationships. When ordering by a related model's fields, the related model is included in the query, performing the necessary join operation. By default, this is an INNER JOIN. You can specify the join type using the join.type option (e.g., 'LEFT', 'RIGHT', or 'INNER'). The orderBy clause specifies the sorting order. If the join type is INNER JOIN, it is not necessary to declare it explicitly; simply setting the field to 'posts' => true will suffice.

  use Lib\Prisma\Classes\Prisma;
    
        $prisma = Prisma::getInstance();
        $users = $prisma->user->findMany([
            'where' => [
                'name' => [
                    'contains' => 'John'
                ]
            ],
            'include' => [
                'posts' => [
                    'join.type' => 'LEFT', // or 'RIGHT' or (default) 'INNER'
                ]
            ],
            'orderBy' => [
                'posts' => [
                    'createdAt' => 'asc' // or 'desc'
                ]
            ]
        ]);
    
        echo "<pre>";
        echo "Users found: " . print_r($users, true);
        echo "</pre>";