Order By Filter
The orderBy filter lets you sort records
in ascending or descending order, either by current model fields or related models.
Sorting Records with orderBy
The orderBy filter allows you to control
the sorting order of your results. You can sort fields from the current model or from
related models, using ascending (asc) or descending (desc) order.
Order by Fields of the Current Model
Sort records based on fields from the main model. You can sort alphabetically, numerically, or by date depending on the field type.
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 by fields from related models. Prisma PHP automatically performs the necessary join (default: INNER JOIN) to access the related field.
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 Related Models with Custom Join Types
You can specify a custom JOIN type when ordering by related model fields (LEFT, RIGHT, or INNER). If using INNER JOIN, you may omit the join.type entirely.
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>";