newInstance() public method

Create a new instance of the given model.
public newInstance ( array $attributes = [], boolean $exists = false ) : static
$attributes array
$exists boolean
return static
 /**
  * Create and return a new Photo model.
  *
  * @param array $attributes
  *
  * @return \App\Photo
  */
 public function create(array $attributes)
 {
     $model = $this->model->newInstance();
     $model->fill($attributes);
     $model->save();
     return $model;
 }
 /**
  * Create a new translation record and return the model.
  *
  * @param $language
  * @param $resource
  * @param $foreignId
  * @param $field
  * @param $value
  * @return Translation
  */
 public function create($language, $resource, $foreignId, $field, $value)
 {
     $model = $this->model->newInstance();
     $model->language = $language;
     $model->foreign_id = $foreignId;
     $model->resource = $resource;
     $model->field = $field;
     $model->value = $value;
     $model->save();
     return $model;
 }
Esempio n. 3
0
 /**
  * Import an Eloquent
  *
  * @param Model $model
  * @param array $relations
  * @param int $batchSize
  * @param callable $callback
  * @internal param $type
  */
 public function import(Model $model, $relations = [], $batchSize = 750, callable $callback = null)
 {
     $batch = 0;
     $asQueryLoggind = $model->getConnection()->logging();
     $model->getConnection()->disableQueryLog();
     while (true) {
         // Increase the batch number
         $batch += 1;
         // Load records from the database
         $records = $model->newInstance()->with($relations)->skip($batchSize * ($batch - 1))->take($batchSize)->get();
         // Break out of the loop if we are out of records
         if (count($records) == 0) {
             break;
         }
         // Call the callback function to provide feedback on the import process
         if ($callback) {
             $callback($batch);
         }
         // Transform each record before sending it to Elasticsearch
         $data = [];
         foreach ($records as $record) {
             $data[] = ['index' => ['_id' => $record->getEsId()]];
             $data[] = $record->transform(!empty($relations));
         }
         // Bulk import the data to Elasticsearch
         $this->bulk($data);
     }
     if ($asQueryLoggind) {
         $model->getConnection()->enableQueryLog();
     }
 }
Esempio n. 4
0
 /**
  * Save a new entity in repository.
  *
  * @param array $attributes
  *
  * @return mixed
  */
 public function create(array $attributes)
 {
     $model = $this->model->newInstance($attributes);
     $model->save();
     $this->resetModel();
     event(new RepositoryEntityCreated($this, $model));
     return $this->parserResult($model);
 }
Esempio n. 5
0
 /**
  *
  */
 protected function refresh()
 {
     if (!$this->model instanceof EloquentModel) {
         $this->model = $this->model->getModel();
     }
     $this->model = $this->model->newInstance();
     $this->criteria = new Collection();
 }
Esempio n. 6
0
 /**
  * Get the first record matching the attributes or create it.
  *
  * @param  array  $attributes
  * @param  array  $values
  * @return \Illuminate\Database\Eloquent\Model
  */
 public function firstOrCreate(array $attributes, array $values = [])
 {
     if (!is_null($instance = $this->where($attributes)->first())) {
         return $instance;
     }
     $instance = $this->model->newInstance($attributes + $values)->setConnection($this->query->getConnection()->getName());
     $instance->save();
     return $instance;
 }
Esempio n. 7
0
 /**
  * Get the first record matching the attributes or create it.
  *
  * @param  array  $attributes
  * @return \Illuminate\Database\Eloquent\Model
  */
 public function firstOrCreate(array $attributes)
 {
     if (!is_null($instance = $this->where($attributes)->first())) {
         return $instance;
     }
     $instance = $this->model->newInstance($attributes);
     $instance->save();
     return $instance;
 }
Esempio n. 8
0
 /**
  * Save a new entity in modal.
  *
  * @param array $attributes
  *
  * @throws ValidatorException
  *
  * @return mixed
  */
 public function create(array $attributes)
 {
     $model = $this->model->newInstance();
     $attributes['user_id'] = User::users('id');
     $model->fill($attributes);
     $model->save();
     $this->resetModel();
     return $model;
 }
Esempio n. 9
0
 /**
  * {@inheritdoc}
  */
 public function set($keys, $value = null)
 {
     if (is_array($keys)) {
         // If we've been given an array, we'll assume they're a
         // key value pair and create a setting for each.
         array_walk($keys, function ($value, $key) {
             $this->set($key, $value);
         });
     } else {
         // We'll try to locate the setting before creating a new instance.
         $model = $this->find($keys) ?: $this->model->newInstance();
         $model->key = $keys;
         $model->value = $value;
         $model->save();
         $this->cache($keys, function () use($model) {
             return $model;
         }, $forget = true);
     }
 }
Esempio n. 10
0
 /**
  * Get the first record matching the attributes or create it.
  *
  * @param  array  $attributes
  * @param  array  $values
  * @return \Illuminate\Database\Eloquent\Model
  */
 public function firstOrCreate(array $attributes, array $values = [])
 {
     $mutatedAttributes = $this->model->newInstance()->forceFill($attributes)->getAttributes();
     if (!is_null($instance = $this->where($mutatedAttributes)->first())) {
         return $instance;
     }
     $instance = $this->model->newInstance($attributes + $values);
     $instance->save();
     return $instance;
 }
 /**
  * Save a new entity in repository
  *
  * @param array $attributes
  * @return mixed|Model
  */
 public function create(array $attributes)
 {
     return $this->wrap(function ($attributes) {
         $model = $this->model->newInstance($attributes);
         /**
          * @var Model $model
          */
         $model->save();
         return $model;
     }, new Action(__METHOD__, func_get_args(), Action::CREATE));
 }
Esempio n. 12
0
 /**
  * Save a new entity in repository
  *
  * @throws ValidatorException
  * @param array $attributes
  * @return mixed
  */
 public function create(array $attributes)
 {
     if (!is_null($this->validator)) {
         $this->validator->with($attributes)->passesOrFail(ValidatorInterface::RULE_CREATE);
     }
     $model = $this->model->newInstance($attributes);
     $model->save();
     $this->resetModel();
     event(new RepositoryEntityCreated($this, $model));
     return $this->parserResult($model);
 }
 /**
  * Assert that a model has been patched.
  *
  * @param Model $model
  *      the model before it was patched.
  * @param array $changedAttributes
  *      the expected changed attributes - key to value pairs.
  * @param string|string[] $unchangedKeys
  *      the keys of the attributes that should not have changed.
  * @return $this
  */
 protected function assertModelPatched(Model $model, array $changedAttributes, $unchangedKeys = [])
 {
     /** We need to ensure values are cast to database values */
     $expected = $model->newInstance($changedAttributes)->getAttributes();
     $attributes = $model->getAttributes();
     foreach ((array) $unchangedKeys as $attr) {
         $expected[$attr] = isset($attributes[$attr]) ? $attributes[$attr] : null;
     }
     $expected[$model->getKeyName()] = $model->getKey();
     return $this->seeModelInDatabase($model, $expected);
 }
Esempio n. 14
0
 public function create(array $attributes)
 {
     $validator = $this->validate($attributes);
     if ($validator->fails()) {
         throw ValidationError::fromValidator($validator);
     }
     $related = $this->fetchRelatedAttributes($attributes);
     $attributes = $this->transformAttributes($attributes);
     $model = $this->model->newInstance($attributes);
     $this->saveModel($model, $related);
     $this->resetModel();
     return $this->parseResult($model);
 }
Esempio n. 15
0
 /**
  * Fill a model with form an elastic hit.
  *
  * @param Model    $model
  * @param array    $attributes
  * @param Relation $parentRelation
  *
  * @return mixed
  */
 public function newFromBuilderRecursive(Model $model, array $attributes = [], Relation $parentRelation = null)
 {
     $instance = $model->newInstance([], $exists = true);
     // fill the instance attributes with checking
     $instance->unguard();
     $instance->fill($attributes);
     $instance->reguard();
     // Load relations recursive
     $this->loadRelationsAttributesRecursive($instance);
     // Load pivot
     $this->loadPivotAttribute($instance, $parentRelation);
     return $instance;
 }
 /**
  * @author bigsinoos <*****@*****.**>
  * Find a model by property
  *
  * @param $property
  * @param $value
  * @throws NotFoundException
  * @throws RepositoryException
  * @return static
  */
 public function findBy($property, $value)
 {
     $model = $this->model->newInstance();
     try {
         $model = $model->where($property, '=', $value)->first();
     } catch (QueryException $e) {
         throw new RepositoryException($e->getMessage());
     }
     if (is_null($model)) {
         throw new NotFoundException("No model with {$property} = {$value} found");
     }
     return $model;
 }
 /**
  * Saves the form and create a new entry in the database
  */
 public function postCreate(Validator $validator, Redirector $redirect, Request $request)
 {
     $rules = $this->getValidationRules('create');
     $validator = $validator->make($request->all(), $rules);
     if ($validator->fails()) {
         return $redirect->back()->withErrors($validator->errors());
     }
     $item = $this->model->newInstance();
     foreach ($rules as $field => $rules) {
         $item[$field] = $request->get($field);
     }
     if ($item->save()) {
         return $redirect->back()->withSuccess('Item has been saved.');
     }
 }
 /**
  * Create and store a new model.
  *
  * @param  array  $attributes
  * @return \Illuminate\Database\Eloquent\Model
  */
 public function create(array $attributes)
 {
     /*
      * Check if is single associate array item.
      */
     if (array_is_assoc(head($attributes))) {
         throw new InvalidArgumentException('Trying to pass multiple items in create() method. Please use createMany() instead.');
     }
     // Unset primary key when $updateWhenIdExists is true, to make sure to create new record in database.
     if (array_is_assoc($attributes) && $this->updateWhenIdExists && array_key_exists($pk = $this->model->getKeyName(), $attributes)) {
         unset($attributes[$pk]);
     }
     $this->validation()->validate('create', $attributes);
     $model = $this->model->newInstance($attributes);
     $this->beforeSaveModel($model);
     $model->save();
     return $model;
 }
Esempio n. 19
0
 /**
  * Update a entity in repository by id
  *
  * @throws ValidatorException
  *
  * @param array $attributes
  * @param       $id
  *
  * @return mixed
  */
 public function update(array $attributes, $id)
 {
     $this->applyScope();
     if (!is_null($this->validator)) {
         // we should pass data that has been casts by the model
         // to make sure data type are same because validator may need to use
         // this data to compare with data that fetch from database.
         $attributes = $this->model->newInstance()->forceFill($attributes)->toArray();
         $this->validator->with($attributes)->setId($id)->passesOrFail(ValidatorInterface::RULE_UPDATE);
     }
     $temporarySkipPresenter = $this->skipPresenter;
     $this->skipPresenter(true);
     $model = $this->model->findOrFail($id);
     $model->fill($attributes);
     $model->save();
     $this->skipPresenter($temporarySkipPresenter);
     $this->resetModel();
     event(new RepositoryEntityUpdated($this, $model));
     return $this->parserResult($model);
 }
Esempio n. 20
0
 /**
  * @param array $dictionary
  * @param array $data
  * @param array $existing
  * @param mixed $parentId
  */
 protected function buildRebuildDictionary(array &$dictionary, array $data, array &$existing, $parentId = null)
 {
     $keyName = $this->model->getKeyName();
     foreach ($data as $itemData) {
         if (!isset($itemData[$keyName])) {
             $model = $this->model->newInstance();
             // We will save it as raw node since tree will be fixed
             $model->rawNode(0, 0, $parentId);
         } else {
             if (!isset($existing[$key = $itemData[$keyName]])) {
                 throw new ModelNotFoundException();
             }
             $model = $existing[$key];
             unset($existing[$key]);
         }
         $model->fill(Arr::except($itemData, 'children'))->save();
         $dictionary[$parentId][] = $model;
         if (!isset($itemData['children'])) {
             continue;
         }
         $this->buildRebuildDictionary($dictionary, $itemData['children'], $existing, $model->getKey());
     }
 }
Esempio n. 21
0
 /**
  * Create a new model instance that is existing recursive.
  *
  * @param  \Illuminate\Database\Eloquent\Model  $model
  * @param  array  $attributes
  * @param  \Illuminate\Database\Eloquent\Relations\Relation  $parentRelation
  * @return static
  */
 public static function newFromBuilderRecursive(Model $model, array $attributes = [], Relation $parentRelation = null)
 {
     $instance = $model->newInstance([], $exists = true);
     $instance->setRawAttributes((array) $attributes, $sync = true);
     // Load relations recursive
     static::loadRelationsAttributesRecursive($instance);
     // Load pivot
     static::loadPivotAttribute($instance, $parentRelation);
     return $instance;
 }
Esempio n. 22
0
 public function getNew(array $attributes = [])
 {
     $this->errors = new MessageBag();
     return $this->modelInstance->newInstance($attributes);
 }
Esempio n. 23
0
 public function getNew($attributes = [])
 {
     return $this->model->newInstance($attributes);
 }
 /**
  * Get a new instance
  *
  * @param  array $attributes
  *
  * @return \Illuminate\Database\Eloquent\Model
  */
 public function instance(array $attributes = [])
 {
     return $this->model->newInstance($attributes);
 }
Esempio n. 25
0
 /**
  * Save a new entity in repository
  *
  * @param array $attributes
  * @return mixed
  */
 public function create(array $attributes)
 {
     $model = $this->model->newInstance($attributes);
     $model->save();
     return $this->parserResult($model);
 }
Esempio n. 26
0
 /**
  * Get a new instance of the model
  *
  * @param array $attributes
  * @return \Illuminate\Database\Eloquent\Model
  */
 public function getNew(array $attributes = array())
 {
     return $this->model->newInstance($attributes);
 }
Esempio n. 27
0
 private function getNew($attributes = array())
 {
     $this->model = $this->model->newInstance($attributes);
 }
Esempio n. 28
0
 public function makeNode()
 {
     $node = $this->model->newInstance();
     $node->forceFill($this->_constraints);
     return $node;
 }
 /**
  * Creates a new instance of the current model
  *
  * @param array $args
  * @return Model
  */
 public function getNew(array $args = [])
 {
     return $this->model->newInstance($args);
 }