Set a given attribute on the model.
public setAttribute ( string $key, mixed $value ) | ||
$key | string | |
$value | mixed |
/** * Removes a model from this relationship type. */ public function remove(Model $model, $sessionKey = null) { if ($sessionKey === null) { $options = $this->parent->getRelationDefinition($this->relationName); if (array_get($options, 'delete', false)) { $model->delete(); } else { /* * Make this model an orphan ;~( */ $model->setAttribute($this->getPlainForeignKey(), null); $model->setAttribute($this->getPlainMorphType(), null); $model->save(); } /* * Use the opportunity to set the relation in memory */ if ($this instanceof MorphOne) { $this->parent->setRelation($this->relationName, null); } else { $this->parent->reloadRelations($this->relationName); } } else { $this->parent->unbindDeferred($this->relationName, $model, $sessionKey); } }
/** * Save user profile. * * @param \Orchestra\Model\User|\Illuminate\Database\Eloquent\Model $user * @param array $input * * @return void */ protected function saving($user, array $input) { $user->setAttribute('email', $input['email']); $user->setAttribute('fullname', $input['fullname']); $this->fireEvent('updating', [$user]); $this->fireEvent('saving', [$user]); $user->saveOrFail(); $this->fireEvent('updated', [$user]); $this->fireEvent('saved', [$user]); }
/** * Removes a model from this relationship type. */ public function remove(Model $model, $sessionKey = null) { if ($sessionKey === null) { // Make this model an orphan ;~( $model->setAttribute($this->getPlainForeignKey(), null); $model->setAttribute($this->getPlainMorphType(), null); $model->save(); } else { $this->parent->unbindDeferred($this->relationName, $model, $sessionKey); } }
/** * Set a given attribute on the model. * Check if filtered first. * * @param string $key * @param mixed $value * @return void */ public function setAttribute($key, $value) { if (in_array($key, $this->filtered)) { $value = \Filter::filter($value, $this->filterReplace); } parent::setAttribute($key, $value); }
/** * Set model attribute. Extend laravel attribute casting mutators by serialized array * @param string $key * @param mixed $value * @return LaravelModel */ public function setAttribute($key, $value) { if ($value !== null && $this->isSerializeCastable($key)) { $value = $this->asSerialize($value); } return parent::setAttribute($key, $value); }
/** * Merges EAV "values" into the entity model, for easy access and * manipulation. * * @param \Illuminate\Database\Eloquent\Model $model * @param \Illuminate\Database\Eloquent\Collection $collection * @return void */ protected function mergeValues(Model $model, Collection $values) { foreach ($values as $value) { $attribute = $value->getRelation($value->getAttributeRelation()); $model->setAttribute($attribute->getAttributeKey(), $value->getValueKey()); } }
/** * Overrides the method to ignore the remember token. */ public function setAttribute($key, $value) { $isRememberTokenAttribute = $key == $this->getRememberTokenName(); if (!$isRememberTokenAttribute) { parent::setAttribute($key, $value); } }
/** * Set a given attribute on the model. * * @param string $key * @param mixed $value * @return $this */ public function setAttribute($key, $value) { if (isset($this->mutations[$key])) { unset($this->mutations[$key]); } return parent::setAttribute($key, $value); }
/** * Attach a model instance to the parent model. * * This adds the field_id value * * @param \Illuminate\Database\Eloquent\Model $model * @return \Illuminate\Database\Eloquent\Model */ public function save(Model $model) { if ($this->fieldId) { $model->setAttribute($this->fieldKey, $this->fieldId); } return parent::save($model); }
/** * Removes a model from this relationship type. */ public function remove(Model $model, $sessionKey = null) { if ($sessionKey === null) { $options = $this->parent->getRelationDefinition($this->relationName); if (array_get($options, 'delete', false)) { $model->delete(); } else { // Make this model an orphan ;~( $model->setAttribute($this->getPlainForeignKey(), null); $model->setAttribute($this->getPlainMorphType(), null); $model->save(); } } else { $this->parent->unbindDeferred($this->relationName, $model, $sessionKey); } }
public function saveAfter(Model $child) { // this is what `$this->relation->save($child)` does // we inline it here to wrap the save() method $child->setAttribute($this->relation->getPlainForeignKey(), $this->relation->getParentKey()); $result = DataSource::make($child)->save(); return $result; }
/** * Convert an instance to an eloquent model object. * * @param \Illuminate\Database\Eloquent\Model $eloquentModel * @param array $name * @return void */ public function toEloquentObject(EloquentModel &$eloquentModel, $name) { // get model data $dict = ['mapping' => $eloquentModel->getMapping()]; foreach ($dict['mapping']['embeddeds'][$name]['attributes'] as $attribute => $column) { $eloquentModel->setAttribute($column, $this->{$attribute}); } }
/** * Override set attributes so we can check for empty strings. * @todo We need a different way to achieve this * @param string $key * @param mixed $value * @return $this */ public function setAttribute($key, $value) { parent::setAttribute($key, $value); // TODO: Change the autogenerated stub if (isset($this->attributes[$key]) && is_string($this->attributes[$key]) && strlen($this->attributes[$key]) === 0) { $this->attributes[$key] = null; } return $this; }
protected function saveModel(Eloquent $model, array $data) { $data = Arr::except($data, ['_token']); foreach ($data as $key => $value) { $model->setAttribute($key, $value); } $model->save(); return $model; }
/** * Add the slug when not explicitly stated. * * @param Model $model * @return void */ public function saving(Model $model) { $hasManuallyAssignedSlug = $model->slug && $model->isDirty('slug'); if (!$model->isDirty('name') || $hasManuallyAssignedSlug) { return; } $slug = str_slug($model->getAttribute($this->fieldToSlug)); $model->setAttribute($this->slugAttribute, $slug); }
/** * @inheritdoc * * @SuppressWarnings(PHPMD.StaticAccess) */ public function setAttribute($key, $value) { // we want to be able to set dates in ISO8601. Laravel has an issue with // build-in format so we convert input manually. // For more see https://github.com/laravel/framework/issues/12203 if (is_string($value) && (in_array($key, $this->getDates()) || $this->isDateCastable($key))) { $value = Carbon::createFromFormat(self::DATE_TIME_FORMAT, $value); } return parent::setAttribute($key, $value); }
/** * Save a new model and attach it to the parent model. * * @param \Illuminate\Database\Eloquent\Model $model * @return \Illuminate\Database\Eloquent\Model */ protected function performInsert(Model $model) { // Create a new key if needed. if (!$model->getAttribute('_id')) { $model->setAttribute('_id', new MongoId()); } $result = $this->query->update(array($this->localKey => $model->getAttributes())); if ($result) { $this->associate($model); } return $result ? $model : false; }
/** * Set a given attribute on the model. * * @param string $key * @param mixed $value * @return $this */ public function setAttribute($key, $value) { if (is_null($value) || !$this->isExtensionCastable($key) || $this->hasSetMutator($key)) { return parent::setAttribute($key, $value); } switch ($this->getCastType($key)) { case 'base64': $value = base64_encode($value); break; } $this->attributes[$key] = $value; return $this; }
/** * @doc inherit */ public function setAttribute($key, $value) { // Let parent handle mutators if ($this->hasSetMutator($key)) { parent::setAttribute($key, $value); } else { if (in_array($key, $this->getDates()) && $value == '') { $this->attributes[$key] = null; } else { parent::setAttribute($key, $value); } } }
/** * Removes a model from this relationship type. */ public function remove(Model $model, $sessionKey = null) { if ($sessionKey === null) { $model->setAttribute($this->getPlainForeignKey(), null); $model->save(); /* * Use the opportunity to set the relation in memory */ if ($this instanceof HasOne) { $this->parent->setRelation($this->relationName, null); } } else { $this->parent->unbindDeferred($this->relationName, $model, $sessionKey); } }
/** * A BelongsTo relation can set using the dynamic setter (still need to be fillable) and boolean are automatically casted * * @param string $key * @param mixed $value * @return $this|bool|null */ public function setAttribute($key, $value) { if (method_exists($this, 'set' . ucfirst(camel_case($key)) . 'Attribute') === false) { if (method_exists($this, $key) === true) { $relation = $this->{$key}(); if (is_a($relation, BelongsTo::class)) { $relation->associate($value); return $this; } } if (array_get($this->casts, $key) == 'boolean') { $value = $value === true ? 1 : ($value === false ? 0 : $value); } } return parent::setAttribute($key, $value); }
/** * Преобразование на запись * * @param string $key * @param mixed $value * @return $this|void */ public function setAttribute($key, $value) { if (isset($this->set_mutators[$key])) { $types = explode('|', $this->set_mutators[$key]); foreach ($types as $type) { switch ($type) { case 'trim': $value = trim($value); break; case 'lower': $value = mb_strtolower($value, 'UTF-8'); break; case 'ucfirst': $firstChar = mb_substr($value, 0, 1, 'UTF-8'); $then = mb_substr($value, 1, mb_strlen($value, 'UTF-8') - 1, 'UTF-8'); $value = mb_strtoupper($firstChar, 'UTF-8') . mb_strtolower($then, 'UTF-8'); break; case 'int': $value = (int) $value; break; case 'real': case 'float': case 'double': $value = (double) str_replace(',', '.', $value); break; case 'db_bool': $value = strtoupper($value); $value = 'Y' == $value || true === $value || '1' === $value || 1 === $value ? 'Y' : 'N'; break; case 'phone': $value = str_replace(['+', ' ', '(', ')', '-'], '', $value); if (10 == strlen($value)) { $value = '7' . $value; } if (11 == strlen($value) && '8' == $value[0]) { $value[0] = '7'; } break; default: break; } } } parent::setAttribute($key, $value); }
/** * Save a new model and attach it to the parent model. * * @param \Illuminate\Database\Eloquent\Model $model * @return \Illuminate\Database\Eloquent\Model */ public function performInsert(Model $model) { // Generate a new key if needed. if ($model->getKeyName() == '_id' and !$model->getKey()) { $model->setAttribute('_id', new MongoId()); } // For deeply nested documents, let the parent handle the changes. if ($this->isNested()) { $this->associate($model); return $this->parent->save(); } $result = $this->getBaseQuery()->update(array($this->localKey => $model->getAttributes())); // Attach the model to its parent. if ($result) { $this->associate($model); } return $result ? $model : false; }
/** * @param Request $request * @param Model $item * @param array $fields * @param $groupName * @param $fieldName * @param bool $mustDeleteFilesInFilesystem * * @throws \Exception * * @return array */ protected function handleField(Request $request, $item, array $fields, $groupName, $fieldName, $mustDeleteFilesInFilesystem = false) { $normalizeGroupName = str_replace('.', '#', $groupName); $contentFromUploadedField = "uploaded-content.{$normalizeGroupName}#{$fieldName}#"; $modelFolder = $this->slug . DIRECTORY_SEPARATOR; $basePath = base_path(DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . config('anavel-crud.uploads_path')); $modelPath = $basePath . $modelFolder; $skip = null; $requestValue = null; if (!empty($fields["{$fieldName}__delete"])) { //We never want to save this field, it doesn't exist in the DB $skip = "{$fieldName}__delete"; //If user wants to delete the existing file if (!empty($request->input("{$groupName}.{$fieldName}__delete"))) { $adapter = new Local($basePath); $filesystem = new Filesystem($adapter); if ($filesystem->has($item->{$fieldName}) && $mustDeleteFilesInFilesystem) { $filesystem->delete($item->{$fieldName}); } $item->setAttribute($fieldName, null); return ['skip' => $skip]; } } if ($request->has($contentFromUploadedField)) { $requestValue = $request->input($contentFromUploadedField); } elseif ($request->hasFile($groupName . '.' . $fieldName)) { $fileName = pathinfo($request->file($groupName . '.' . $fieldName)->getClientOriginalName(), PATHINFO_FILENAME); $extension = pathinfo($request->file($groupName . '.' . $fieldName)->getClientOriginalName(), PATHINFO_EXTENSION); $fileName = uniqid() . '_' . slugify($fileName); if (!empty($extension)) { $fileName .= '.' . $extension; } $request->file($groupName . '.' . $fieldName)->move($modelPath, $fileName); $requestValue = $modelFolder . $fileName; } elseif (!empty($request->file($groupName . '.' . $fieldName)) && !$request->file($groupName . '.' . $fieldName)->isValid()) { throw new \Exception($request->file($groupName . '.' . $fieldName)->getErrorMessage()); } //Avoid losing the existing filename if the user doesn't change it: if (empty($requestValue) && !empty($item->{$fieldName})) { $requestValue = $item->{$fieldName}; } return ['requestValue' => $requestValue, 'skip' => $skip]; }
public function setAttribute($key, $value) { // Check if key is actually a relationship /** @var Relation $relation */ if (method_exists($this, $key)) { // If so, convert scalars and arrays to instances of the correct model if (isset($value) && !$value instanceof Model) { /** @var Relation $relation */ $relation = $this->{$key}(); // Convert arrays to instances of the correct model if (is_array($value)) { if (isset($value[0]) && is_scalar($value[0])) { } else { $value = $relation->getRelated()->newInstance($value); } } if ($relation instanceof BelongsTo) { $relation->associate($value); } else { $this->setRelation($key, $value); } return; } } parent::setAttribute($key, $value); }
/** * Attach a model instance to the parent model. * * @param \Illuminate\Database\Eloquent\Model $model * @return \Illuminate\Database\Eloquent\Model */ public function save(Model $model) { $model->setAttribute($this->getPlainForeignKey(), $this->getParentKey()); return $model->save() ? $model : false; }
/** * Attach a model instance to the parent model. * * @param \Illuminate\Database\Eloquent\Model $model * @return \Illuminate\Database\Eloquent\Model */ public function save(Model $model) { $model->setAttribute($this->getPlainMorphType(), $this->morphClass); return parent::save($model); }
/** * Updates a model with the latest permissions, links, and fields * * @param \Illuminate\Database\Eloquent\Model $model * @param \Frozennode\Administrator\Fields\Factory $fieldFactory * @param \Frozennode\Administrator\Actions\Factory $actionFactory * * @return \Illuminate\Database\Eloquent\Model */ public function updateModel($model, FieldFactory $fieldFactory, ActionFactory $actionFactory) { //set the data model to the active model $this->setDataModel($model->find($model->getKey())); //include the item link if one was supplied if ($link = $this->getModelLink()) { $model->setAttribute('admin_item_link', $link); } //set up the model with the edit fields new data $model->setAttribute('administrator_edit_fields', $fieldFactory->getEditFieldsArrays(true)); //set up the new actions data $model->setAttribute('administrator_actions', $actionFactory->getActionsOptions(true)); $model->setAttribute('administrator_action_permissions', $actionFactory->getActionPermissions(true)); return $model; }
/** * Set a given attribute on the model. * * @param string $key * @param mixed $value */ public function setAttribute($key, $value) { // Convert _id to MongoId. if ($key == '_id' and is_string($value)) { $builder = $this->newBaseQueryBuilder(); $value = $builder->convertKey($value); } elseif (str_contains($key, '.')) { if (in_array($key, $this->getDates()) && $value) { $value = $this->fromDateTime($value); } array_set($this->attributes, $key, $value); return; } parent::setAttribute($key, $value); }
/** * @param string $key * @param mixed $value * @return $this * @throws SetRelationException */ public function setAttribute($key, $value) { if (in_array($key, $this->getDates()) && $value) { if (!$value instanceof Carbon && !$value instanceof \DateTime) { $value = new Carbon($value); $this->attributes[$key] = $value; return; } } return parent::setAttribute($key, $value); }