Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/ApiResources/Transformers/LocationTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class LocationTransformer extends TransformerAbstract

public function transform(Location $location): array
{
return $this->mergesIdAttribute($location);
return $this->mergesIdAttribute($location, ['settings' => $location->settings]);
// return $this->mergesIdAttribute($location);
}

public function includeMedia(Location $location): ?Item
Expand Down
67 changes: 64 additions & 3 deletions src/Classes/AbstractRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ protected function setModelAttributes($model, $saveData)
$this->modelsToSave[] = $model;

$singularTypes = ['belongsTo', 'hasOne', 'morphOne'];
$pluralTypes = ['hasMany', 'morphMany'];
foreach ($saveData as $attribute => $value) {
if ($attribute === $model->getKeyName() || !$model->isFillable($attribute)) {
continue;
Expand All @@ -243,14 +244,74 @@ protected function setModelAttributes($model, $saveData)
continue;
}

$isNested = ($attribute == 'pivot' || (
$isSingularNested = ($attribute == 'pivot' || (
$model->hasRelation($attribute) &&
in_array($model->getRelationType($attribute), $singularTypes)
));

if ($isNested && is_array($value) && $model->{$attribute}) {
// Handle singular nested relations (belongsTo, hasOne, morphOne)
if ($isSingularNested && is_array($value) && $model->{$attribute}) {
$this->setModelAttributes($model->{$attribute}, $value);
} elseif (!starts_with($attribute, '_')) {
continue;
}

// Handle plural nested relations (hasMany, morphMany)
if ($model->hasRelation($attribute) && in_array($model->getRelationType($attribute), $pluralTypes) && is_array($value)) {
try {
$relation = $model->{$attribute}();
$relatedModel = method_exists($relation, 'getModel') ? $relation->getModel() : (method_exists($relation, 'getRelated') ? $relation->getRelated() : null);
if ($relatedModel === null) {
continue;
}

$relatedKey = $relatedModel->getKeyName();
$fkName = $model->getForeignKey();

$existing = $relation->get()->keyBy($relatedKey)->all();

foreach ($value as $attrs) {
if (!is_array($attrs)) {
continue;
}

if (!empty($attrs[$relatedKey]) && isset($existing[$attrs[$relatedKey]])) {
$rel = $existing[$attrs[$relatedKey]];
$rel->fill($attrs);
} else {
$rel = new ($relatedModel::class)($attrs);
}

// Queue wrapper that will set parent's FK and save related model after parent saved
$this->modelsToSave[] = new class($rel, $model, $fkName) {
protected $rel;
protected $parent;
protected $fk;

public function __construct($rel, $parent, $fk)
{
$this->rel = $rel;
$this->parent = $parent;
$this->fk = $fk;
}

public function save()
{
if (empty($this->rel->{$this->fk}) && $this->parent->getKey()) {
$this->rel->{$this->fk} = $this->parent->getKey();
}

$this->rel->save();
}
};
}
} catch (\Throwable $ex) {
// If relation introspection fails, skip gracefully
}

continue;
}

if (!starts_with($attribute, '_')) {
$model->{$attribute} = $value;
}
}
Expand Down
Loading