Find Unique Or Throw

Meet the next generation of documentation. AI-native, beautiful, out-of-the-box, and built for developers and teams.

Purpose

The findUniqueOrThrow method retrieves a single unique record based on the provided criteria. If no matching record exists, an exception is thrown. This ensures that the result is never null and prevents silent failures in critical workflows.

Parameters

  • 'where' – Unique identifier such as a primary key or unique field.
  • 'select' – Optional fields to include.
  • 'include' – Optional related models to include.
  • 'omit' – Optional fields to exclude.

Return Value

Returns a single object representing the uniquely matched record. If no record matches, an exception is thrown, ensuring the return value is never null.

Exception Handling

  • Throws an exception if no record matches the unique filter.
  • Throws if both select and include are provided.
  • Throws if 'where' is missing or invalid.
  • Throws if unsupported keys exist in the query input.

Features

  • Guarantees exactly one record returned or throws immediately.
  • Fails fast when no match exists.
  • Supports field selection and relation inclusion.
  • Allows field omission for cleaner structured output.

Example Usage

use Lib\Prisma\Classes\Prisma;

$prisma = Prisma::getInstance();

try 
    $user = $prisma->user->findUniqueOrThrow([
        'where' => [
            'id' => 1
        ],
        'select' => [
            'id' => true,
            'name' => true,
            'email' => true
        ]
    ]);

    echo "<pre>"; 
    print_r($user); 
    echo "</pre>";

catch (Exception $e) 
    echo "Error: " . $e->getMessage();


    

Behavior When No Record Found

If the specified record is not found, the method immediately throws an exception. This is useful when the existence of the record is required for the operation to continue.

try 
    $user = $prisma->user->findUniqueOrThrow([
        'where' => ['email' => 'missing@example.com']
    ]);
 catch (Exception $e) 
    echo "User not found.";