Create a new record from the parent Model and new related records with it.

@param  array  $attributes
@param  array  $relations
@return \Sgpatil\Orientdb\Eloquent\Model
325518
public function createWith(array $attributes, array $relations) {
        // Collect the model attributes and label in the form of ['label' => $label, 'attributes' => $attributes]
        // as expected by the Query Builder.
        $attributes = $this->prepareForCreation($this->model, $attributes);
        $model = ['label' => $this->model->getTable(), 'attributes' => $attributes];

        /**
         * Collect the related models in the following for as expected by the Query Builder:
         *
         *  [
         *       'label' => ['Permission'],
         *       'relation' => [
         *           'name' => 'photos',
         *           'type' => 'PHOTO',
         *           'direction' => 'out',
         *       ],
         *       'values' => [
         *           // A mix of models and attributes, doesn't matter really..
         *           ['url' => '', 'caption' => ''],
         *           ['url' => '', 'caption' => '']
         *       ]
         *  ]
         */
        $related = [];
        foreach ($relations as $relation => $values) {
            $name = $relation;
            // Get the relation by calling the model's relationship function.
            if (!method_exists($this->model, $relation))
                throw new QueryException("The relation method $relation() does not exist on " . get_class($this->model));

            $relationship = $this->model->$relation();
            // Bring the model from the relationship.
            $relatedModel = $relationship->getRelated();

            // We will first check to see what the dev have passed as values
            // so that we make sure that we have an array moving forward
            // In the case of a model Id or an associative array or a Model instance it means that
            // this is probably a One-To-One relationship or the dev decided not to add
            // multiple records as relations so we'll wrap it up in an array.
            if (!is_array($values) || Helpers::isAssocArray($values) || $values instanceof Model)
                $values = [$values];

            $label = $relationship->getRelated()->getTable();
            $direction = $relationship->getEdgeDirection();
            $type = $relationship->getRelationType();

            // Hold the models that we need to attach
            $attach = [];
            // Hold the models that we need to create
            $create = [];
            // Separate the models that needs to be attached from the ones that needs
            // to be created.
            foreach ($values as $value) {
                // If this is a Model then the $exists property will indicate what we need
                // so we'll add its id to be attached.
                if ($value instanceof Model and $value->exists === true) {
                    $attach[] = $value->getKey();
                }
                // Next we will check whether we got a Collection in so that we deal with it
                // accordingly, which guarantees sending an Eloquent result straight in would work.
                elseif ($value instanceof Collection) {
                    $attach = array_merge($attach, $value->lists('id'));
                }
                // Or in the case where the attributes are neither an array nor a model instance
                // then this is assumed to be the model Id that the dev means to attach and since
                // Orientdb node Ids are always an int then we take that as a value.
                elseif (!is_array($value) && !$value instanceof Model) {
                    $attach[] = intval($value);
                }
                // In this case the record is considered to be new to the market so let's create it.
                else
                    $create[] = $this->prepareForCreation($relatedModel, $value);
            }

            $relation = compact('name', 'type', 'direction');
            $related[] = compact('relation', 'label', 'create', 'attach');
        }

        $result = $this->query->createWith($model, $related);

        $models = $this->resultsToModels($this->model->getConnectionName(), $result);
        return (!empty($models)) ? reset($models) : null;
    }
329796
public function create($attributes = array())
    {
        $entity = $this->build($attributes);

        // to SQL.
        $criteria = $this->criteria();
        $sql = $criteria->toInsertSQL($attributes);

        // query.
        $sth = $this->query($sql, $criteria->getParams(), Database::TARGET_MASTER);
        if ($sth->isSuccess()) {
            $entity->exists = true;
            $entity->setPrimaryValue($sth->lastInsertId());
            return $entity;
        }
    }
1290096
public function loadCount($relations)
    {
        $relations = is_string($relations) ? func_get_args() : $relations;

        $this->newCollection([$this])->loadCount($relations);

        return $this;
    }
1226748
public function forceCreate(array $attributes)
    {
        $instance = $this->model->newInstance()->setConnection(
            $this->query->getConnection()->getName()
        );

        return $this->model->unguarded(function () use ($attributes, $instance) {
            return $instance->create($attributes);
        });
    }
21349
public function create(array $attributes)
    {
        $attributes = Collection::make($attributes);

        $attr = $attributes->except('features', 'pictures')->merge([
            'features' => \Antvel\Features\Parser::toJson($attributes->get('features')),
            'category_id' => $attributes->get('category'),
            'price' => $attributes->get('price') * 100,
            'cost' => $attributes->get('cost') * 100,
            'status' => $attributes->get('status'),
            'created_by' => auth()->user()->id,
            'updated_by' => auth()->user()->id,
            'tags' => $attributes->get('name'),
        ])->all();

        $product = Product::create($attr);

        $this->createPicturesFor($product, $attributes);

        return $product;
    }
160978
public function relations(array $relations)
    {
        foreach ($relations as $column1 => $column2) {
            $this->relation($column1, $column2);
        }

        return $this;
    }
1040212
public function create(array $attributes)
    {
        $entity = $this->getNew($attributes);

        if ($entity->save()) {
            $this->flushCache();

            return $entity;
        }

        return false;
    }
311434
protected function getRelationsArray(array $relations): array
    {
        $relationsArray = [];
        foreach ($relations as $relation) {
            $key = $relation->getName();

            $relationsArray[$key] = (new self($relation->getJory(), $this->minified))->get();
        }

        return $relationsArray;
    }
548253
public function make(array $attributes = [])
    {
        $instance = $this->related->newInstance($attributes);
        $this->setForeignAttributesForCreate($instance);
        return $instance;
    }
560238
public function newModelInstance($attributes = [])
    {
        return $this->model->newInstance($attributes)->setConnection(
            $this->query->getConnection()
        );
    }
377401
public function loadCount($relations)
    {
        if ($this->isEmpty()) {
            return $this;
        }

        $models = $this->first()->newModelQuery()
            ->whereKey($this->modelKeys())
            ->select($this->first()->getKeyName())
            ->withCount(...func_get_args())
            ->get();

        $attributes = Arr::except(
            array_keys($models->first()->getAttributes()),
            $models->first()->getKeyName()
        );

        $models->each(function ($model) use ($attributes) {
            $this->find($model->getKey())->forceFill(
                Arr::only($model->getAttributes(), $attributes)
            )->syncOriginalAttributes($attributes);
        });

        return $this;
    }
1166243
public function setAttributes($attributes)
    {
        if ($attributes === null) {
            $this->attributes = [];

            return $this;
        }

        if (is_array($attributes)) {
            $this->attributes = $attributes;

            return $this;
        }

        $attributes = explode(',', $attributes);
        $attributes = $this->relationsToArray($attributes);

        $this->attributes = $attributes;

        return $this;
    }
1018276
protected function assignRelationships($entity, $attributes)
    {
        $modelAttributes = $this->model->getAttributes($entity);

        // We'll filter through all of the columns, and check
        // to see if there are any defined relationships. If there
        // are, then we'll need to create those records as well.

        foreach ($modelAttributes as $columnName => $value) {
            if ($relationship = $this->findRelation($value)) {
                $entity[$columnName] = $this->fetchRelationId($relationship, $columnName, $attributes);
            }
        }

        return $entity;
    }
160794
public function initRelation(array $models, $relation)
    {
        foreach ($models as $model) {
            $model->setRelation($relation, $this->getDefaultFor($model));
        }

        return $models;
    }
1366572
public function parentRelations(array $relations): ModelList
    {
        $this->main['parents'] = [];

        foreach ($relations as $key => $value) {

            if (is_string($key)) {
                $parent = [
                    'relation' => $key,
                    'field'    => $value,
                ];
            } else {
                $parent = [
                    'relation' => $value,
                    'field'    => $value,
                ];
            }

            $this->main['parents'][] = $parent;
        }

        return $this;
    }
351630
public function create(array $attributes = null)
    {
        $this->doQuery($this->createQuery($attributes));
        $this->{static::$PKFieldName[0]} = static::db()->lastInsertId();
        if ($attributes) {
            $this->fillProperties($attributes);
        }

        return $this;
    }
1074539
public function createRoot(array $attributes)
    {
        $attributes[$this->getParentIdName()] = null;
        $root = parent::create($attributes);

        if (!$root[$this->getRootIdName()]) {
            $root[$this->getRootIdName()] = $root->getId();
            $root->save();
        }

        return $root;

    }