Find Unique
Meet the next generation of documentation. AI-native, beautiful, out-of-the-box, and built for developers and teams.
Purpose
The findUnique method retrieves a single unique record
using a primary key or another uniquely constrained field. It ensures precise data retrieval and
returns null when no matching record exists.
Parameters
- 'where' (required) – Unique identifier (primary key or unique field).
- 'select' – Fields to return.
- 'include' – Related models to include.
- 'omit' – Fields to exclude.
Return Value
Returns a single object representing the matching record.
If no matching record exists, null is returned.
Exception Handling
- Throws an exception if
'where'is missing or not an array. - Throws an exception if no valid unique field is found in
'where'. - Throws an exception if
selectandincludeare used together. - Throws an exception if unsupported keys are present.
Features
- Designed for exact match queries using unique fields.
- Supports selective field retrieval.
- Supports including related models.
- Validates conditions and structure before execution.
Example Usage: By ID
use Lib\Prisma\Classes\Prisma;
$prisma = Prisma::getInstance();
$user = $prisma->user->findUnique([
'where' => [
'id' => 1
]
]);
echo "<pre>";
print_r($user);
echo "</pre>";
Example Usage: Unique by Email
$user = $prisma->user->findUnique([
'where' => [
'email' => 'john@example.com'
],
'select' => [
'id' => true,
'name' => true,
'email' => true
]
]);
Example Usage: Including Related Model
$user = $prisma->user->findUnique([
'where' => [
'id' => 1
],
'include' => [
'profile' => true
]
]);