Fill a model using the available attributes.

@param  Model   $model
@param  array[] $insertedPKs
@return void
327549
protected function fillModel(Model $model, array $insertedPKs)
    {
        $attributes = $this->mergeAttributes($model);

        // To maximize the number of attributes already set in the
        // model when accessing it from closure custom attributes,
        // we'll first set all the non-closure attributes.
        $closureAttributes = [];

        foreach ($attributes as $key => $value) {
            // Don't use is_callable() here since it returns true for values
            // that happen to be function names, e.g. country code "IS".
            if ($value instanceof \Closure) {
                $closureAttributes[$key] = $value;
            } else {
                $model->$key = $value;
            }
        }

        // We'll set the remaining attributes as we evaluate the closures,
        // so that the model will have the return values of previous closures already set.
        foreach ($closureAttributes as $key => $value) {
            $model->$key = $value($model, $insertedPKs);
        }
    }
817923
private function createInsertObj(Model $model)
    {
        $metadata = $model->getMetadata();
        $insert = [
            $this->getIdentifierKey()   => $this->convertId($model->getId()),
        ];
        if (true === $metadata->isChildEntity()) {
            $insert[$this->getPolymorphicKey()] = $metadata->type;
        }
        return $this->appendChangeSet($model, $insert, $this->getCreateChangeSetHandler());
    }
548319
public function fill(Identifiable $model, array $attributes)
    {
        $this->publish('filling', $model, $attributes);
        $filtered = $this->toModelAttributes($model, $attributes);
        $model->fill($filtered);
        $this->publish('filled', $model, $attributes);

        return true;
    }
226315
protected function getInsertedPKs(Model $createdModel, $insertedCount)
    {
        $primaryKeyName = $createdModel->getKeyName();

        return $createdModel->newQueryWithoutScopes()
                            ->take($insertedCount)
                            ->latest($primaryKeyName)
                            ->pluck($primaryKeyName)
                            ->all();
    }
103271
protected function appendRelatedModelsToModelData(array $model)
    {
        $relationships = array_merge(
            array_get($model, 'relationships.normal'),
            array_get($model, 'relationships.reverse')
        );

        $model['related_models'] = [];

        foreach ($relationships as $name => $relationship) {

            $relatedModelId = $relationship['model'];

            if (isset($model['related_models'][ $relatedModelId ])) continue;

            $model['related_models'][ $relatedModelId ] = $this->data['models'][ $relatedModelId ];
        }

        return $model;
    }
1022894
public function push(Relational $model)
	{
		// Index by primary key if possible, otherwise plain incremental array
		// Also check to see if that key already exists.  If so, we'll just start
		// appending items to the array.  This will result in a mixed array and
		// subsequent items will not be seekable.
		if ($model->getPkValue() && (!is_array($this->rows) || !array_key_exists($model->getPkValue(), $this->rows)))
		{
			$this->rows[$model->getPkValue()] = $model;
		}
		else
		{
			$this->rows[] = $model;
		}
	}
622645
public function insertModels(Models $models)
    {
        foreach ($models as $model) {
            $this->dispatchBeforeEvent($model, Event::INSERT);
            $this->dispatchBeforeEvent($model, Event::SAVE);
        }

        $this->insertAll()->executeModels($models);

        foreach ($models as $model) {
            $model
                ->resetOriginals()
                ->setState(State::SAVED);

            $this->dispatchAfterEvent($model, Event::INSERT);
            $this->dispatchAfterEvent($model, Event::SAVE);
        }

        return $this;
    }
10165
protected function setForeignAttributesForCreate(Model $model)
	{
		$model->{$this->getPlainForeignKey()} = $this->getParentKey();

		$model->{last(explode('.', $this->morphType))} = $this->morphClass;
	}
857291
public function create(Model $model)
    {
        $metadata = $model->getMetadata();
        $insert = $this->createInsertObj($model);
        $this->getQuery()->executeInsert($metadata, $insert);
        return $model;
    }
556735
public function setModel(Model $model)
    {
        parent::setModel($model);

        if ($model->exists) {
            $this->setInstance($model);
            $this->loadRelationValues();
        }
    }
610652
protected function fill()
    {
        $this->primaryKeys = $this->blueprint->primaryKey();

        // Process columns
        foreach ($this->blueprint->columns() as $column) {
            $this->parseColumn($column);
        }

        if (! $this->loadRelations) {
            return;
        }

        foreach ($this->blueprint->relations() as $relation) {
            $model = $this->makeRelationModel($relation);
            $belongsTo = new BelongsTo($relation, $this, $model);
            $this->relations[$belongsTo->name()] = $belongsTo;
        }

        foreach ($this->factory->referencing($this) as $related) {
            $factory = new ReferenceFactory($related, $this);
            $references = $factory->make();
            foreach ($references as $reference) {
                $this->relations[$reference->name()] = $reference;
            }
        }
    }
161322
protected function pickRelatedIds(array $insertedPKs)
    {
        return $this->generator->randomElements($insertedPKs[$this->relatedClass], $this->getQuantity($insertedPKs));
    }
1002003
def insert_model(self, model, *, upsert=None):
    
    pkname = model.primary_key_name
    include_keys = set(model.attrs.keys()).difference(model.exclude_keys_sql)

    if model.primary_key_is_auto:
      if pkname in include_keys:
        include_keys.remove(pkname)
    else:
      if isinstance(pkname, str):
        include_keys.add(pkname)
      else:
        include_keys.update(set(pkname))

    data = model.to_dict(include_keys=include_keys)
    returnings = []

    if model.primary_key_is_auto:
      returnings.append(pkname)
    if model.timestamps:
      returnings.extend(ts_name for ts_name in model.timestamps if ts_name)

    returning = ", ".join(returnings)
    cr = self.insert(model.table_name, data, returning=returning, upsert=upsert)

    if self.core.supports_returning_syntax:
      if returning:
        rec = cr.fetchone()
        if rec:
          for idx, attr_name in enumerate(returnings):
            setattr(model, attr_name, rec[idx])
    else:
      if model.primary_key_is_auto:
        setattr(model, model.primary_key_name, cr.lastrowid)

    return model
1143703
public function setAddedPkColumns(array $addedPkColumns)
    {
        $this->addedPkColumns = [];
        foreach ($addedPkColumns as $addedPkColumn) {
            $this->addAddedPkColumn($addedPkColumn->getName(), $addedPkColumn);
        }
    }
1318157
public function submit($model, $values)
    {
        $exists = $this->exists($model, $values);

        if (!$exists) {
            $exists = $this->insert($model, $values);
        }

        return $exists;
    }
1305190
public function setup(Model $model, $settings = array()) {
        $settings = array_merge($this->_defaults, $settings);
        $this->settings[$model->alias] = $settings;

        // Store the default model validate set
        if ($model->validate) {
            if (empty($model->validations)) {
                $model->validations = array();
            }

            $model->validations[$settings['defaultSet']] = $model->validate;
            $model->validate = array();
        }
    }
313540
public function addEagerConstraints(array $models)
    {
        if (is_array($this->ownerKey)) { //Check for multi-columns relationship
            $keys = [];

            foreach ($this->ownerKey as $key) {
                $keys[] = $this->related->getTable().'.'.$key;
            }

            $this->query->whereIn($keys, $this->getEagerModelKeys($models));
        } else {
            // We'll grab the primary key name of the related models since it could be set to
            // a non-standard name and not "id". We will then construct the constraint for
            // our eagerly loading query so it returns the proper models from execution.
            $key = $this->related->getTable().'.'.$this->ownerKey;
            $this->query->whereIn($key, $this->getEagerModelKeys($models));
        }
    }