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 true or nested select arrays.
  • Supports deeply nested relations.
  • Cannot be used with select at the same level.
  • Supports special meta fields such as _count for 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 include when related model data is needed immediately.
  • Use nested select to limit fields on included models.
  • Do not combine select and include at the top level.
  • Use _count to obtain relation sizes without loading full related records.