Performs a Model creation commit and persists to the database.

@param   Model   $model
@return  Model
3335
private function doCommitCreate(Model $model)
    {
        $this->dispatchLifecycleEvent(Events::preCreate, $model);

        $this->getPersisterFor($model->getType())->create($model);
        $model->getState()->setNew(false);
        // Should the model always reload? Or should the commit be assumed correct and just clear the new/dirty state?
        $model->reload();

        $this->dispatchLifecycleEvent(Events::postCreate, $model);
        return $model;
    }
670530
public function createFromModel(Model $model)
    {
        $this->model = $model;
        $this->query = $this->model->newQuery();

        return $this;
    }
1083491
public function saved(Model $model)
    {
        if ($model::$__es_enable && $model->shouldIndex())
        {
            Queue::push('Iverberk\Larasearch\Jobs\ReindexJob', $this->findAffectedModels($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;
    }
1265567
protected function insertModelAsFirstRoot(Model &$model)
    {
        /** @var \T4\Orm\Model $class */
        $class = get_class($model);
        $tableName = $class::getTableName();
        /** @var \T4\Dbal\Connection $connection */
        $connection = $class::getDbConnection();

        $query = (new Query())
            ->select('MIN(__lft)')->from($tableName);
        $minLft = (int)$connection->query($query)->fetchScalar();

        $width = $model->getTreeWidth();
        $this->expandTreeBeforeLft($connection, $tableName, $minLft, $width);

        if (!$model->isNew()) {
            $model->refreshTreeColumns();
            $query = (new Query("
                UPDATE `" . $tableName . "`
                SET
                    `__lft` = `__lft` - :lft + :min,
                    `__rgt` = `__rgt` - :lft + :min,
                    `__lvl` = `__lvl` - :lvl
                WHERE `__lft` >= :lft AND `__rgt` <= :rgt
                "))
                ->params([':min' => $minLft, ':width'=> $width, ':lft' => $model->__lft, ':rgt' => $model->__rgt, ':lvl' => $model->__lvl]);
            $connection->execute($query);
            $this->removeFromTreeByElement($model);
        }

        $model->__lft = $minLft;
        $model->__rgt = $model->__lft + $width;
        $model->__lvl = 0;
        $model->__prt = 0;
    }
1026427
public function save(BaseMysqlModel $model) {
        $create_vars = $this->onSave_pre((array)$model);
        $sql = $this->buildInsertStatement($create_vars);

        $id = $this->connection_manager->executeWithReconnect(function($mysql_dbh) use ($sql, $create_vars) {
            $sth = $mysql_dbh->prepare($sql);
            $result = $sth->execute(array_values($create_vars));
            return $mysql_dbh->lastInsertId(); 
        });

        $model['id'] = $id;

        return $model;
    }
295706
public function save($model)
    {
        if (isset($model->{$this->key})) {
            return $this->update($model);
        }
        
        return $this->create($model);
    }
1024470
public function push(AbstractModel $model)
    {
        $this->validateAdd($model);
        if (true === $this->willAdd($model)) {
            return $this;
        }
        if (true === $this->willRemove($model)) {
            $this->evict('removed', $model);
            $this->set('models', $model);
            return $this;
        }
        if (true === $this->hasOriginal($model)) {
            return $this;
        }
        $this->set('added', $model);
        $this->set('models', $model);
        return $this;
    }
440366
protected function insertModelAsLastRoot(Model $model)
    {
        /** @var \T4\Orm\Model $class */
        $class = get_class($model);
        $tableName = $class::getTableName();
        /** @var \T4\Dbal\Connection $connection */
        $connection = $class::getDbConnection();

        $query = (new Query())
            ->select('MAX(__rgt)')
            ->from($tableName);
        $maxRgt = (int)$connection->query($query)->fetchScalar();

        if ($model->isNew()) {

            $model->__lft = $maxRgt + 1;
            $model->__rgt = $model->__lft + 1;
            $model->__lvl = 0;
            $model->__prt = null;

        } else {

            $query = (new Query())
                ->update()
                ->table($tableName)
                ->values([
                    '__lft' => '__lft + :max - :lft + 1',
                    '__rgt' => '__rgt + :max - :lft + 1',
                    '__lvl' => '__lvl - :lvl',
                ])
                ->where('__lft>=:lft AND __rgt<=:rgt')
                ->params([':max' => $maxRgt, ':lft' => $model->__lft, ':rgt' => $model->__rgt, ':lvl' => $model->__lvl]);
            $connection->execute($query);

            $this->removeFromTreeByElement($model);

            // TODO: calculate new __lft, __rgt!
            $model->refreshTreeColumns();
            $model->__lvl = 0;
            $model->__prt = null;
        }
    }
1221260
public function creating(Model $model)
    {
        if ($model instanceof SingleSiteInterface) {
            $site = $this->router->getActiveSite() ?: $this->site->findDefault();

            $model->{SingleSiteInterface::ATTR_SITE} = ($site ? $site->getId() : null);
        }
    }
1107807
protected function add(AbstractModel $model)
    {
        if (true === $this->has($model)) {
            return $this;
        }
        $this->validateAdd($model);
        if (true === $model->getState()->is('empty')) {
            $this->loaded = false;
        }

        $key = $model->getCompositeKey();
        $this->models[$key] = $model;
        $this->modelKeyMap[] = $key;

        if (false === $this->hasOriginal($model)) {
            $this->original[$key] = $model;
        }
        return $this;
    }
797181
protected function reindexModel(Model $model)
    {
        $mapping = $this->option('mapping') ? json_decode(File::get($this->option('mapping')), true) : null;

        $this->info('---> Reindexing ' . get_class($model));

        $model->reindex(
            $this->option('relations'),
            $this->option('batch'),
            $mapping,
            function ($batch)
            {
                $this->info("* Batch ${batch}");
            }
        );
    }
1196356
public function saved(Model $model)
    {
        $attributes = $this->processAttributes(Request::input('attributes'));
        $this->syncAttributes($model, $attributes);
    }
485273
public function save(Model $model)
    {
        $model->setAttribute($this->getForeignKeyName(), $this->getParentKey());

        return $model->save() ? $model : false;
    }
1172768
public function link(Model $model) {
        $this->getRelation($this->getAlias(get_class($model)))->link($model);

        return $this;
    }
93386
public function create(Model $model) : Model
    {
        return $model->load(['attachment', 'tags', 'taxonomies'])
            ->setAttribute('category', $model->taxonomies->map(function ($item) {
                return $item->id;
            })->toArray());
    }
824966
public function save(Model $model)
    {
        $saved = $model->save();

        if ($saved) {
            app('cache')->flush();
        }

        return $saved;
    }