Create a Collection of new instances of the related model.

@param  iterable  $records
@return \Illuminate\Database\Eloquent\Collection
345986
public function createMany(iterable $records)
    {
        $instances = $this->related->newCollection();

        foreach ($records as $record) {
            $instances->push($this->create($record));
        }

        return $instances;
    }
801919
public function collection($collection, $wrap = true)
    {
        $collection = $this->database->selectCollection($collection);
        $wrap && $collection = new Collection($collection);

        return $collection;
    }
950192
public static function bulkCreate(array $records, array $params = [])
    {
        if (empty($records)) {
            throw new BadRequestException('There are no record sets in the request.');
        }

        $response = [];
        $errors = false;
        $rollback = array_get_bool($params, ApiOptions::ROLLBACK);
        $continue = array_get_bool($params, ApiOptions::CONTINUES);

        if ($rollback) {
            //	Start a transaction
            DB::beginTransaction();
        }

        foreach ($records as $key => $record) {
            try {
                $response[$key] = static::createInternal($record, $params);
            } catch (\Exception $ex) {
                $errors = true;
                $response[$key] = $ex;
                // track the index of the error and copy error to results
                if ($rollback || !$continue) {
                    break;
                }
            }
        }

        if ($errors) {
            $msg = "Batch Error: Not all requested records could be created.";
            if ($rollback) {
                DB::rollBack();
                $msg .= " All changes rolled back.";
            }

            throw new BatchException($response, $msg);
        }

        if ($rollback) {
            DB::commit();
        }

        return $response;
    }
431929
public function multiCreate($records)
    {
        $ids = [];

        foreach ($records as $record) {
            try {
                $createdRecord = $this->create($record);
                $ids[] = $createdRecord[$this->getIdentifier()];
            } catch (\Throwable $e) {
                // TODO: need to log record that was not created
                continue;
            }
        }

        return $ids;
    }
1087129
protected function mergeRecords(array $records)
    {
        foreach ($records as $key => $record) {
            if (null === $id = array_get($record, static::ID_FIELD)) {
                throw new InternalServerErrorException('No ' .
                    static::ID_FIELD .
                    ' field found in supplied record(s). Cannot merge record(s) for PATCH operation.');
            }

            $rs = $this->getConnection()->getDocument($this->transactionTable, $id);
            $record = array_merge($rs, $record);
            $records[$key] = $record;
        }

        return $records;
    }
1371592
protected static function inLineArray(array $records)
    {

        $values = [];

        foreach ($records as $record)
            $values = array_merge($values, array_values($record));

        return $values;
    }
219534
public function getHeaderRowFromRecords(Collection $records): array
    {
        $titles = collect(Arr::dot($records->first()->toArray()))->keys()->map(
            function ($key) {
                $key = str_replace('.', ' ', $key);

                return Str::ucfirst($key);
            }
        );

        return $titles->toArray();
    }
76196
public function setOrder(array $records)
    {
        $config = $this->getConfig();
        $table = $this->_table;

        $table->removeBehavior('Sequence');

        $return = $table->getConnection()->transactional(
            function ($connection) use ($table, $records) {
                $order = $this->_config['start'];
                $field = $this->_config['order'];

                /** @var string $primaryKeyField */
                $primaryKeyField = $table->getPrimaryKey();
                foreach ($records as $record) {
                    if (is_scalar($record)) {
                        $record = [$primaryKeyField => $record];
                    }

                    if (is_array($record)) {
                        $record = $table->newEntity($record, [
                            'fields' => array_keys($record),
                            'validate' => false,
                            'accessibleFields' => [
                                $primaryKeyField => true,
                            ],
                        ]);
                        $record->isNew(false);
                        $record->setDirty($primaryKeyField, false);
                    }

                    $record->setAccess($field, true);
                    $record->set($field, $order++);

                    $r = $table->save(
                        $record,
                        ['atomic' => false, 'checkRules' => false]
                    );
                    if ($r === false) {
                        return false;
                    }
                }

                return true;
            }
        );

        $table->addBehavior('ADmad/Sequence.Sequence', $config);

        return $return;
    }
845433
public function new(array $record = [])
    {
        $record = $this->prepareBaseRecord($record);

        return $this->cleanClone()->addPristineRecord($record, ['isCasted' => true])->markAsNew();
    }
91662
protected function formatRecordsList(array $records)
    {

        $result = [];
        foreach ($records as $id => $attributes) {
             if (! is_array($attributes)) {
                list($id, $attributes) = [$attributes, []];
            }
            $result[$id] = $attributes;
        }
        return $result;
    }
608097
public function all()
	{
		return new RecordSet($this->db, $this->table, $this->primary, $this->select);
	}
993330
protected function loadModels($typeKey, RecordSetInterface $records)
    {
        $models = [];
        foreach ($records as $record) {
            $models[] = $this->loadModel($typeKey, $record);
        }
        return $models;
    }
1246422
public function setFields($fields)
    {
        if ($fields instanceof RecordFields) {
            $this->fields = $fields;
        } else {
            if (is_array($fields)) {
                $fields       = new RecordFields($fields);
                $this->fields = $fields;
            }
        }
        return $this;
    }
1088441
protected function processRecords(array $records)
    {
        $record = new Record();
        $formatter = $this->getFormatter();
        if ($this->isInstanceOf($formatter, FormatterInterface::class)) {
            $formatter->formatRecords($records, $record);
        }

        $this->write($record);

        return $this->bubble;
    }
762668
public function groupRecords($records)
    {
        if (!is_array($records) || empty($records)) {
            return;
        }

        $groups = array('year' => array());

        foreach ($records as $r) {
            $data = $r->getData($this->get('id'));

            $timestamp = DateTimeObj::get('U', $data['value']);
            $info = getdate($timestamp);

            $year = $info['year'];
            $month = ($info['mon'] < 10 ? '0' . $info['mon'] : $info['mon']);

            if (!isset($groups['year'][$year])) {
                $groups['year'][$year] = array(
                    'attr' => array('value' => $year),
                    'records' => array(),
                    'groups' => array()
                );
            }

            if (!isset($groups['year'][$year]['groups']['month'])) {
                $groups['year'][$year]['groups']['month'] = array();
            }

            if (!isset($groups['year'][$year]['groups']['month'][$month])) {
                $groups['year'][$year]['groups']['month'][$month] = array(
                    'attr' => array('value' => $month),
                    'records' => array(),
                    'groups' => array()
                );
            }

            $groups['year'][$year]['groups']['month'][$month]['records'][] = $r;
        }

        return $groups;
    }
296549
protected function preCleanRecords($records)
    {
        $new = [];
        foreach ($records as $record) {
            if (property_exists($record, $this->transactionTable)) {
                $cleaned = (array)$record->{$this->transactionTable};
                unset($cleaned[static::ID_FIELD]);
                if (property_exists($record, '_id')) {
                    $cleaned = array_merge([static::ID_FIELD => $record->_id], $cleaned);
                }
            } else {
                $cleaned = (array)$record;
            }
            $new[] = $cleaned;
        }

        return $new;
    }
1372299
private function transformRecords(RecordCollection $records, array $transformers, $context)
    {
        foreach ($transformers as $transformer) {
            if ($transformer instanceof PorterAware) {
                $transformer->setPorter($this);
            }

            $records = $transformer->transform($records, $context);
        }

        return $records;
    }