Connect Operator Overview

This section describes the available relation operators such as create, createMany, connect, connectOrCreate, disconnect, update, and updateMany. Each operator allows managing relations and nested writes for advanced data modeling.

Operators

  • create — Creates a new record with the provided data.
  • createMany — Creates multiple records at once. Does NOT support nested relations.
  • connect — Links an existing record without modifying its internal data.
  • connectOrCreate — Connects if the record exists; otherwise it creates it and then connects.
  • disconnect — Removes the relation but does not delete the related record.
  • update — Updates fields on a single related record.
  • updateMany — Updates multiple related records.

NOTE: Unlike create, the createMany operator cannot create nested related records.

Usage Examples

Creating a User with Related Profile using create

use Lib\Prisma\Classes\Prisma;

$prisma = Prisma::getInstance();
$newUser = $prisma->user->create([
    'data' => [
        'name' => 'John Doe',
        'email' => 'john.doe@example.com',
        'password' => 'password123',
        'profile' => [
            'create' => [
                'bio' => 'Software Developer',
            ]
        ]
    ]
]);

echo "<pre>";
echo "New user created with profile: " . print_r($newUser, true);
echo "</pre>";

Creating a User with Post + Categories + Tags

$newUser = $prisma->user->create([
    'data' => [
        'name' => 'John Doe',
        'email' => 'john.doe@example.com',
        'password' => 'password123',
        'post' => [
            'create' => [
                [
                    'title' => 'My First Post',
                    'content' => 'This is the content of my first post.',
                    'categories' => [
                        'name' => 'PHP Frameworks',
                        'tags' => [
                            'create' => [
                                ['name' => 'PHP'],
                                ['name' => 'Laravel']
                            ]
                        ]
                    ]
                ],
                [
                    'title' => 'My Second Post',
                    'content' => 'This is the content of my second post.',
                    'categories' => [
                        'name' => 'JavaScript Frameworks',
                        'tags' => [
                            'create' => [
                                ['name' => 'JavaScript'],
                                ['name' => 'React']
                            ]
                        ]
                    ]
                ]
            ]
        ]
    ]
]);

Creating a User with Multiple Posts using createMany

$newUser = $prisma->user->create([
    'data' => [
        'name' => 'John Doe',
        'email' => 'john.doe@example.com',
        'password' => 'password123',
        'posts' => [
            'createMany' => [
                ['title' => 'My First Post', 'content' => 'This is my first post'],
                ['title' => 'My Second Post', 'content' => 'This is my second post']
            ]
        ]
    ]
]);

Creating a User with Existing Roles using connect

$newUser = $prisma->user->create([
    'data' => [
        'name' => 'John Doe',
        'email' => 'john.doe@example.com',
        'password' => 'password123',
        'roles' => [
            'connect' => [
                'name' => 'Admin'
            ]
        ]
    ]
]);

Update User using disconnect (Explicit Relation)

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

Update User using disconnect (Implicit Relation Requires ID)

$updatedUser = $prisma->user->update([
    'where' => ['id' => 'someUserId'],
    'data' => [
        'email' => 'new.email@example.com',
        'userRole' => [
            'disconnect' => ['idRole' => 2]
        ]
    ]
]);

Update User using connectOrCreate

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

Update User using update

$updatedUser = $prisma->user->update([
    'where' => ['id' => 'someUserId'],
    'data' => [
        'email' => 'updated.email@example.com',
        'name' => 'John Doe',
        'profile' => [
            'update' => ['bio' => 'Senior Software Developer']
        ]
    ]
]);

Update User using updateMany

$updatedUsers = $prisma->user->update([
    'where' => ['id' => 'someUserId'],
    'data' => [
        'email' => 'updated.email@example.com',
        'name' => 'John Doe',
        'post' => [
            'updateMany' => [
                ['title' => 'My First Post', 'content' => 'Updated content #1'],
                ['title' => 'My Second Post', 'content' => 'Updated content #2']
            ]
        ]
    ]
]);