Update

The update method modifies a single record by its unique identifier, supporting relational updates, field validation, and response shaping using select or include.

Purpose

The update method updates one specific record from the database using a unique identifier. It supports dynamic field updates, relational actions such as connect, disconnect, update, updateMany, and ensures data consistency through transactions.

Parameters

  • where — Required. Identifies the record to update using a unique field.
  • data — Required. Fields to update, including relational operations.
  • select — Optional. Limits the returned fields.
  • include — Optional. Includes related models in the response.

Return Value

Returns the updated record as an associative array or object. If include or select is used, the returned structure follows the requested shape.

Error Handling

  • Throws an exception if both select and include are used simultaneously.
  • Throws an exception if the record does not exist.
  • Rolls back the transaction if any update fails.
  • Validates relational actions to prevent invalid nested updates.

Example Usage

Basic Update

use Lib\Prisma\Classes\Prisma;

$prisma = Prisma::getInstance();
$updatedUser = $prisma->user->update([
    'where' => ['id' => 'someUserId'],
    'data' => ['email' => 'new.email@example.com']
]);

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

    

Update with disconnect

$updatedUser = $prisma->user->update([
    'where' => ['id' => 'someUserId'],
    'data' => [
        'email' => 'new.email@example.com',
        'userRole' => [
            'disconnect' => true // or ['name' => 'Admin']
        ]
    ]
]);
    

Update using connectOrCreate

$updatedUser = $prisma->user->update([
    'where' => ['id' => 'someUserId'],
    'data' => [
        'userRole' => [
            'connectOrCreate' => [
                'where' => ['name' => 'Admin'],
                'create' => ['name' => 'Admin']
            ]
        ]
    ]
]);
    

Update with relations, updateMany & connect

$updatedUser = $prisma->user->update([
    'where' => ['id' => 'someUserId'],
    'data' => [
        'email' => 'new.email@example.com',

        'userRole' => [
            'connect' => ['name' => 'Admin']
        ],

        'product' => [
            'update' => [
                'name' => 'New Product',
                'description' => 'New Product Description',
                'quantity' => 10,
                'price' => 100,
                'store' => 10
            ]
        ],

        'post' => [
            'updateMany' => [
                'where' => ['authorId' => 'zbDD5936Ox5z2hwCo5-lU'],
                'data' => [
                    'title' => 'New Post Title 3',
                    'content' => 'New Post Content 3',
                    'published' => true
                ]
            ]
        ]
    ]
]);
    

Shaping the Response with include

$updatedUser = $prisma->user->update([
    'where' => ['id' => 'someUserId'],
    'data' => [
        'email' => 'new.email@example.com',
        'userRole' => [
            'connect' => ['name' => 'Admin']
        ]
    ],
    'include' => ['userRole' => true]
]);
    

Shaping the Response with select

$updatedUser = $prisma->user->update([
    'where' => ['id' => 'someUserId'],
    'data' => [
        'email' => 'new.email@example.com',
        'userRole' => [
            'connectOrCreate' => [
                'where' => ['name' => 'Admin'],
                'create' => ['name' => 'Admin']
            ]
        ]
    ],
    'select' => [
        'id' => true,
        'name' => true,
        'email' => true,
        'userRole' => true
    ]
]);