Find First
Meet the next generation of documentation. AI-native, beautiful, out-of-the-box, and built for developers and teams.
Purpose
The findFirst method retrieves the first User record
that matches the given filter criteria. It supports filtering, ordering, selective field retrieval,
and related model inclusion. Unlike findMany, this method returns only a single record or
null if no match is found.
Parameters
- 'where' – Filter conditions.
- 'orderBy' – Sorting rules applied before selecting the first record.
- 'cursor' – Starting point for cursor-based pagination.
- 'select' – Fields to include in the result.
- 'include' – Related models to include.
- 'distinct' – Ensures distinct results.
- 'omit' – Fields to exclude from the result.
Return Value
Returns a single object if a matching record is found, or null otherwise.
For example, for the User model, it returns a User object or null.
Exception Handling
- Throws an exception if
includeandselectare used at the same time. - Throws an exception if
whereis not an array or is empty. - Throws an exception if invalid keys are present in the filter.
Features
- Returns the first matching record.
- Supports nested relational filtering.
- Allows precise field selection and omission.
- Supports cursor-based advanced pagination.
- Includes related models when required.
Example Usage
use Lib\Prisma\Classes\Prisma;
$prisma = Prisma::getInstance();
$user = $prisma->user->findFirst([
'where' => [
'email' => [
'equals' => 'john@example.com'
]
],
'orderBy' => [
'createdAt' => 'desc'
],
'select' => [
'id' => true,
'name' => true,
'email' => true
]
]);
echo "<pre>";
print_r($user);
echo "</pre>";
Behavior When No Record Found
When no record matches the filter, findFirst returns null.
This allows safe conditional handling:
if (!$user)
echo "User not found.";
else
echo "User email: " . $user->email;