Create

The create method inserts a new record into the database, supporting related records, validation, transactions, and shaping responses with select, include, and format.

Purpose

The create method inserts a new User record into the database. It supports validation, transactions, related model creation, and flexible response shaping. Ensures data integrity through automatic transaction handling.

Parameters

  • data – Required associative array with the values to insert.
  • select – Shape the returned fields.
  • include – Include related models.
  • format – Choose array or object.

Return Value

Returns the newly created record as an associative array or an object depending on the format parameter. Related models are included when specified using select or include.

Exception Handling

  • Throws exception if data is missing or invalid.
  • Throws if select and include are used together.
  • Throws when database operation fails; transaction rolls back.

Example Usage

Creating a New User Record

use Lib\Prisma\Classes\Prisma;

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

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

    

Creating a User with Related Profile

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 Multiple Posts

use Lib\Prisma\Classes\Prisma;

$prisma = Prisma::getInstance();
$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.']
            ]
        ]
    ]
]);

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

    

Creating a User with Roles (connectOrCreate)

use Lib\Prisma\Classes\Prisma;

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

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

    

Creating a User with Existing Roles (connect)

use Lib\Prisma\Classes\Prisma;

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

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

    

Shaping the Response with include

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',
            ]
        ],
        'roles' => [
            'connectOrCreate' => [
                'where' => ['name' => 'Admin'],
                'create' => ['name' => 'Admin'],
            ],
        ],
    ],
    'include' => [
        'profile' => true,
        'roles' => true
    ]
]);

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

    

Shaping the Response with select

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',
            ]
        ],
        'roles' => [
            'connectOrCreate' => [
                'where' => ['name' => 'Admin'],
                'create' => ['name' => 'Admin'],
            ],
        ],
        'post' => [
            'createMany' => [
                ['title' => 'Hello World', 'content' => 'This is my first post!'],
                ['title' => 'Second Post', 'content' => 'This is my second post!'],
            ]
        ],
    ],
    'select' => [
        'id' => true,
        'name' => true,
        'email' => true,
        'roles' => [
            'select' => [
                'id' => true,
                'name' => true,
            ],
        ],
        'profile' => [
            'select' => [
                'id' => true,
                'bio' => true,
            ],
        ],
        'post' => [
            'select' => [
                'id' => true,
                'title' => true,
                'content' => true,
            ],
        ],
    ]
]);

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