public function bindModel($attribute, $model) { if (is_callable($model)) { $model = $model(); } $this->model = $model; $this->attribute = $attribute; if ($this->model) { $relationType = $this->model->getRelationType($attribute); $this->isMulti = $relationType == 'attachMany' || $relationType == 'morphMany'; $this->isBound = true; } }
/** * Sets a data collection to a model attributes, relations will also be set. * @param array $saveData Data to save. * @param Model $model Model to save to * @return array The collection of models to save. */ protected function setModelAttributes($model, $saveData) { $this->modelsToSave[] = $model; if (!is_array($saveData)) { return; } $singularTypes = ['belongsTo', 'hasOne', 'morphOne']; foreach ($saveData as $attribute => $value) { $isNested = $attribute == 'pivot' || $model->hasRelation($attribute) && in_array($model->getRelationType($attribute), $singularTypes); if ($isNested && is_array($value)) { $this->setModelAttributes($model->{$attribute}, $value); } elseif ($value !== FormField::NO_SAVE_DATA) { $model->{$attribute} = $value; } } }
/** * Sets a data collection to a model attributes, relations will also be set. * @param array $saveData Data to save. * @param Model $model Model to save to * @return array The collection of models to save. */ protected function setModelAttributes($model, $saveData) { $this->modelsToSave[] = $model; if (!is_array($saveData)) { return; } $singularTypes = ['belongsTo', 'hasOne', 'morphOne']; foreach ($saveData as $attribute => $value) { if (is_array($value) && $model->hasRelation($attribute) && in_array($model->getRelationType($attribute), $singularTypes)) { $this->setModelAttributes($model->{$attribute}, $value); } else { $model->{$attribute} = $value; } } }