Method Documentation: findFirst

Purpose

The findFirst method fetches the first record from the 'Users' table that matches specified criteria. It is optimized for queries where only the earliest or most relevant record is needed, supporting advanced features like sorting, relationship inclusion, and specific field selection.

Parameters

  • array $criteria = [] - An associative array specifying the query criteria. Supports keys for filtering ('where', etc.), 'orderBy' for sorting, 'select' for field selection, and 'include' for relation inclusion. Note: 'include' and 'select' cannot be used simultaneously.
  • bool $format = false- Optional. Specifies the format of the returned data ('false' or 'true') by default is array, but can also be set to 'true' for object format.

Return Value

Returns the first matching record as an associative array or a user object, depending on the specified format. Includes any related records if specified in the 'include' parameter. Returns an empty array if no matching record is found, ensuring consistent return types.

Exception Handling

Throws an \Exception if 'include' and 'select' are used simultaneously, or if any other query construction errors occur, ensuring safe and predictable method behavior.

Example Usage: Default Call

  use Lib\Prisma\Classes\Prisma;
            
    $prisma = Prisma::getInstance();
    $firstUser = $prisma->user->findFirst();

    echo "<pre>";
    echo "User found: " . print_r($firstUser, true);
    echo "</pre>";

Example Usage: Applying Filters

  use Lib\Prisma\Classes\Prisma;
            
    $prisma = Prisma::getInstance();
    $firstUser = $prisma->user->findFirst([
        'orderBy' => ['name' => 'asc'],
    ]);

    echo "<pre>";
    echo "User found: " . print_r($firstUser, true);
    echo "</pre>";

Example Usage: Filtering with Conditions

  use Lib\Prisma\Classes\Prisma;
            
    $prisma = Prisma::getInstance();
    $firstUser = $prisma->user->findFirst([
        'where' => ['name' => 'John Doe'],
        'orderBy' => ['name' => 'asc'],
    ]);

    echo "<pre>";
    echo "User found: " . print_r($firstUser, true);
    echo "</pre>";

Example Usage: Advanced Filtering with String Matching

  use Lib\Prisma\Classes\Prisma;
            
    $prisma = Prisma::getInstance();
    $firstUser = $prisma->user->findFirst([
        'where' => ['name' => ['contains' => 'Doe']],
        'orderBy' => ['name' => 'asc'],
    ]);

    echo "<pre>";
    echo "User found: " . print_r($firstUser, true);
    echo "</pre>";

Example Usage: Using include

  use Lib\Prisma\Classes\Prisma;
            
    $prisma = Prisma::getInstance();
    $firstUser = $prisma->user->findFirst([
        'where' => ['email' => 'example@email.com'],
        'orderBy' => ['name' => 'asc'],
        'include' => ['profile' => true]
    ]);

    echo "<pre>";
    echo "User found: " . print_r($firstUser, true);
    echo "</pre>";

Example Usage: Using select

  use Lib\Prisma\Classes\Prisma;
            
    $prisma = Prisma::getInstance();
    $firstUser = $prisma->user->findFirst([
        'where' => ['name' => ['contains' => 'Doe']],
        'orderBy' => ['name' => 'asc'],
        'select' => [
            'id' => true, 
            'name' => true, 
            'email' => true,
            'profile' => [
                'select' => [
                    'bio' => true
                ]
            ]
        ]
    ]);

    echo "<pre>";
    echo "User found: " . print_r($firstUser, true);
    echo "</pre>";