Include Fields
Fetch related records using the include parameter for eager-loading
relationships and associated data.
Including Related Models with include
The include parameter allows you to load related models
alongside the main record. This is useful for eager-loading relations such as one-to-one,
one-to-many, or many-to-many associations.
- Accepts an associative array where each key is a relation name.
- Values can be
trueor nestedselectarrays. - Supports deeply nested relations.
- Cannot be used with
selectat the same level. - Supports special meta fields such as
_countfor relation counts.
Example Usage: Basic Include
use Lib\Prisma\Classes\Prisma;
$prisma = Prisma::getInstance();
$users = $prisma->user->findMany([
'include' => [
'profile' => true
]
]);
echo "<pre>";
print_r($users);
echo "</pre>";
Example Usage: Nested Include with Select
$users = $prisma->user->findMany([
'include' => [
'profile' => [
'select' => [
'bio' => true
]
]
]
]);
Example Usage: Relation Count with _count
$users = $prisma->user->findMany([
'include' => [
'_count' => [
'select' => [
'posts' => true
]
]
]
]);
Best Practices
- Use
includewhen related model data is needed immediately. - Use nested
selectto limit fields on included models. - Do not combine
selectandincludeat the top level. - Use
_countto obtain relation sizes without loading full related records.