本文整理汇总了PHP中Illuminate\Database\Eloquent\Model::newInstance方法的典型用法代码示例。如果您正苦于以下问题:PHP Model::newInstance方法的具体用法?PHP Model::newInstance怎么用?PHP Model::newInstance使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Illuminate\Database\Eloquent\Model
的用法示例。
在下文中一共展示了Model::newInstance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: create
/**
* 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;
}
开发者ID:bexarcreative,项目名称:using-rate-limiting-on-method-calls-with-laravel,代码行数:14,代码来源:PhotoRepository.php
示例2: create
/**
* 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;
}
示例3: import
/**
* 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();
}
}
示例4: create
/**
* 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);
}
示例5: refresh
/**
*
*/
protected function refresh()
{
if (!$this->model instanceof EloquentModel) {
$this->model = $this->model->getModel();
}
$this->model = $this->model->newInstance();
$this->criteria = new Collection();
}
示例6: firstOrCreate
/**
* 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;
}
示例7: firstOrCreate
/**
* 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;
}
示例8: create
/**
* 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;
}
示例9: set
/**
* {@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);
}
}
示例10: firstOrCreate
/**
* 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;
}
示例11: create
/**
* 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));
}
示例12: create
/**
* 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);
}
示例13: assertModelPatched
/**
* 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);
}
示例14: create
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);
}
示例15: newFromBuilderRecursive
/**
* 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;
}