Fill an instance of a model with all known fields.

@param \Illuminate\Database\Eloquent\Model $instance
@return mixed
@todo support more relationship types, such as polymorphic ones!
332056
protected function fill(Model $instance): bool
	{
		$input = $this->getInput();
		$model_fields = $this->getFields($instance);
		$before_relations = [];
		$after_relations = [];
		$instance_model = get_class($instance);
		$safe_instance = new $instance_model;

		$input = ($safe_instance->getIncrementing()) ? array_except($input, [$instance->getKeyName()]) : $input;

		foreach ($input as $key => $value) {
			if (($relation = $this->isRelation($instance, $key, $instance_model)) && $this->isFillable($key)) {
				$relation_type = get_class($relation);

				switch ($relation_type) {
					case BelongsTo::class:
						$before_relations[] = [
							'relation' => $relation,
							'value' => $value,
						];
						break;
					case HasOne::class:
					case HasMany::class:
					case BelongsToMany::class:
						$after_relations[] = [
							'relation' => $relation,
							'value' => $value,
						];
						break;
				}
			} elseif ((in_array($key, $model_fields) || $instance->hasSetMutator($key)) && $this->isFillable($key)) {
				$instance->{$key} = $value;
			}
		}

		unset($safe_instance);

		$this->applyRelations($before_relations, $instance);
		$instance->save();
		$this->applyRelations($after_relations, $instance);

		return true;
	}
1325266
protected function makeGenericUpdate(Model $model)
	{
		// Get first fillable key that is not the models primary key.
		$field = array_first(
			$model->getFillable(), function ($key, $value) use ($model) {
			return ($value !== $model->getKeyName());
		}
		);

		switch (gettype($model->{$field})) {
			case 'string':
				$model->{$field} = $model->{$field} . ' updated';
				break;
			case 'boolean':
				$model->{$field} = ! $model->{$field};
				break;
			case 'integer':
			case 'NULL':
			default:
				$model->{$field} = $model->{$field} + 1;
		}

		return $this;
	}
493642
public static function find($model)
    {
        if (property_exists(self::className(), 'instance')) {
            if (static::$instance instanceof static) {
                /**
                 * @var QueryBuilder $instance
                 */
                $instance = static::$instance;
                $instance->setDao(Dao::component())
                    ->setConditions()
                    ->setGroupFields()
                    ->setOrders()
                    ->setLimit()
                    ->setSelect()
                    ->setAsArray();

                return $instance;
            } else {
                $instance = new static($model);
                $instance->is_single = true;
                return (static::$instance = $instance);
            }
        }
        return null;
    }
31683
protected function getDefaultFor(Model $model)
    {
        if (! $this->withDefault) {
            return;
        }

        $instance = $this->related->newInstance()->setAttribute(
            $this->getForeignKeyName(),
            $model->getAttribute($this->localKey)
        );

        if (is_callable($this->withDefault)) {
            return call_user_func($this->withDefault, $instance) ?: $instance;
        }

        if (is_array($this->withDefault)) {
            $instance->forceFill($this->withDefault);
        }

        return $instance;
    }
150410
protected function setModelForElements(Model $model)
    {
        $this->getElements()->each(function ($element) use ($model) {
            $element = $this->getElementContainer($element);

            if ($element instanceof WithModelInterface) {
                $element->setModel($model);
            }
        });

        return $this;
    }
839526
public function save(Model $model) {
        if ($model->isPrimaryKey($this->foreignKey)) {
            $model->save();
            $this->parent->{$this->localKey} = $model->{$this->foreignKey};
            $this->parent->save();
            return $this;
        }
        $model->{$this->foreignKey} = $this->parent->{$this->localKey};
        $model->save();
        return $this;
    }
60556
public function form($instance)
    {
        $fields = $this->getModel()->getFields();

        // Check fields
        if (empty($fields)) {
            return;
        }

        // Add Title field from default mode
        $new_title = [
            'special' => [
                'id' => 'title',
                'title' => Translate::t('widget.fields.title'),
            ],
        ];

        // Add Title field from `olympus-text-field` component
        if (class_exists('\\GetOlympus\\Field\\Text')) {
            $new_title = \GetOlympus\Field\Text::build('title', [
                'title' => Translate::t('widget.fields.title'),
            ]);
        }

        // Add title on 1st place
        array_unshift($fields, $new_title);
        unset($new_title);

        $vars = [];

        // Get fields
        foreach ($fields as $field) {
            if (!$field) {
                continue;
            }

            // Build contents
            if (is_array($field) && isset($field['special'])) {
                $ctn = (array) $field['special'];
                $hasId = true;
            } else {
                $ctn = (array) $field->getModel()->getContents();
                $hasId = (boolean) $field->getModel()->getHasId();
            }

            // Check fields
            if (empty($ctn) || !$hasId) {
                continue;
            }

            // Does the field have an ID
            if (!isset($ctn['id']) || empty($ctn['id'])) {
                continue;
            }

            // Value
            $id = isset($ctn['id']) ? $ctn['id'] : '';
            $value = !empty($id) && isset($instance[$id]) ? $instance[$id] : '';

            // Id and name
            $ctn['id'] = $this->get_field_id($id);
            $ctn['name'] = $this->get_field_name($id);

            // Get render field
            $vars['fields'][] = $field->render($ctn, [
                'template' => 'widget',
                'widget_value' => $value
            ], false);
        }

        // Render view
        Render::view('widget.html.twig', $vars, 'widget');
    }
306243
public function model($model)
    {
        #$model = ucfirst($model).'Model';
        $modelPath = self::$instance->app->findModel($model);
        $modelClass = "App\\Model\\".$model;
        $this->model = new $modelClass();
        self::$instance->modelService = new ModelService();
        self::$instance->modelService->initialize(self::$instance->app);
        return $this->model;
        //$appModel->handleModel($controller, $method);
    }
736053
public function update(Model $model)
    {
        $metadata = $model->getMetadata();
        $criteria = $this->getQuery()->getRetrieveCritiera($metadata, $model->getId());
        $update = $this->createUpdateObj($model);

        if (empty($update)) {
            return $model;
        }

        $this->getQuery()->executeUpdate($metadata, $model->getStore(), $criteria, $update);
        return $model;
    }
309276
public function bootstrapModel($model)
    {
        if (! property_exists($model, 'rules')) {
            throw new ModelSanityException('Missing rules property for model '.get_class($model));
        }

        if (! $model instanceof AdminModel) {
            throw new ModelSanityException('Model '.get_class($model).' must be instanceof '.AdminModel::class);
        }

        $imageFields = $model->getImageFields();

        if (! is_array($imageFields)) {
            throw new ModelSanityException('No Image fields defined in config for model '.get_class($model));
        }

        // Check for fields collisions
        foreach ($imageFields as $imageFieldName => $imageField) {
            $imageMetaFields = $model->getImageMetaFields($imageFieldName);
            foreach ($imageMetaFields as $metaFieldName => $options) {
            }
        }

        $model->setRetinaFactor(config('ignicms.images.retina_factor'));

        foreach ($imageFields as $fieldName => $field) {
            $model->prepareImageRules($model, 'rules', $fieldName, $field);
        }
    }
580920
public function add_relationship($instance, $property = 'fields') {
    $this->__belongs_to = array('property' => $property, 'instance' => $instance);

    // Add relationship to all individual fields as well.
    foreach ($this as $item) {
      if ($item->has_property($property)) {
        $item->add_relationship($instance, $property);
      }
    }
  }
422927
public static function instance($instance = null)
	{
		if ($instance !== null)
		{
			if ( ! array_key_exists($instance, static::$_instances))
			{
				return false;
			}

			return static::$_instances[$instance];
		}

		if (static::$_instance === null)
		{
			static::$_instance = static::forge();
		}

		return static::$_instance;
	}
716772
public function add_model($class, $instance = null, $method = 'set_form_fields')
	{
		$this->fieldset->add_model($class, $instance, $method);

		return $this;
	}
1226740
function save(Model $instance, array $data)
    {
        // First transform data, passing false as a second parameter to allow partial objects.
        // This is important: this save() can be the second one called in the same request
        // for any field which formatter required a delay in his execution.
        $data = $this->applyTransformers($data, false);

        // Then handle manually ignored attributes...
        if(count($this->ignoredAttributes)) {
            $data = collect($data)->filter(function ($value, $attribute) {
                return array_search($attribute, $this->ignoredAttributes) === false;
            })->all();
        }

        // Finally call updater
        return app(EloquentModelUpdater::class)
            ->initRelationshipsConfiguration($this->getFormListFieldsConfiguration())
            ->update($instance, $data);
    }
192761
public function create(Model $model)
    {
        $data['model'] = $this->createModelData($model);

        $modelResult = $this->getRequest()->request(
            'POST',
            $this->getRequestUrl('models'),
            $data
        );

        return $this->getModelFromResult($modelResult);
    }
524572
public static function objectsManager($instance = null)
    {
        if (!$instance) {
            $className = get_called_class();
            $instance = new $className();
        }

        if (class_exists($managerClass = self::getManagerClass())) {
            return new $managerClass($instance, $instance->getConnection());
        }

        return new TreeManager($instance, $instance->getConnection());
    }
1075199
public function toType($model)
    {
        list($instance, $class) = $this->instanceAndClass($model);

        if ($type = $this->getFromCache($class)) {
            return $type;
        }

        $config = $this->buildConfig($instance, $class);

        $objectType = $this->typeFactory->toType("object|class:$class");

        $objectType->provideKeysBy(function () use (&$config) {
            return $this->typeFactory->toType($config);
        });

        return $this->putIntoCacheAndReturn($objectType);
    }