createManyAndReturn

The createManyAndReturn method inserts multiple records and returns the newly created entries with support for field filtering and relation inclusion.

Purpose

The createManyAndReturn method allows bulk insertion of multiple rows while returning the inserted records. It supports filtering fields via select, including related models through include, and skipping duplicates. The entire operation is executed inside a transaction to maintain data consistency.

Parameters

  • data — Required. A non-empty list of associative arrays representing the rows to insert.
  • skipDuplicates — Optional boolean. When true, duplicate rows are ignored.
  • select — Optional. Specifies which fields to return.
  • include — Optional. Includes related models in the returned result.
  • omit — Optional. Excludes specific fields from the returned result.

Return Value

Returns an array containing the created model objects. The output is shaped according to select, include, or omit. If no rows are inserted, an empty array is returned.

Exception Handling

  • Throws InvalidArgumentException if data is missing or empty.
  • Throws InvalidArgumentException if any item in data is not an associative array.
  • Throws LogicException if using relation actions inside rows.
  • Throws LogicException if using both select and include.
  • Throws Exception on enum validation or missing required fields.
  • Automatically rolls back the transaction if anything fails.

Example Usage

Inserting Multiple Records and Returning Selected Fields

use Lib\Prisma\Classes\Prisma;

$prisma = Prisma::getInstance();

$users = $prisma->user->createManyAndReturn([
    'data' => [
        [
            'name' => 'Alice',
            'email' => 'alice@example.com'
        ],
        [
            'name' => 'Bob',
            'email' => 'bob@example.com'
        ]
    ],
    'select' => [
        'id' => true,
        'name' => true
    ],
    'skipDuplicates' => true
]);

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

    

Behavior When No Records Inserted

$users = $prisma->user->createManyAndReturn([
    'data' => [],
]);

// Result: []
print_r($users);