Skip to content

Commit

Permalink
deal with non-revisable hasmany fields
Browse files Browse the repository at this point in the history
  • Loading branch information
edalzell committed Jan 8, 2025
1 parent aa5cb0c commit eba51da
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/Relationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@

class Relationships
{
public function __construct(protected Model $model, protected array $values = []) {}
public function __construct(protected Model $model, protected array $values = [], protected array $except = []) {}

public static function for(Model $model): self
{
return new static($model);
}

public function except(array $except): self
{
$this->except = $except;

return $this;
}

public function with(array $values): self
{
$this->values = $values;
Expand All @@ -31,6 +38,7 @@ public function save(): void
{
$this->model->runwayResource()->blueprint()->fields()->all()
->reject(fn (Field $field) => $field->visibility() === 'computed' || ! $field->get('save', true))
->reject(fn (Field $field) => in_array($field->handle(), $this->except))
->filter(fn (Field $field) => $field->fieldtype() instanceof HasManyFieldtype)
->each(function (Field $field): void {
$relationshipName = $this->model->runwayResource()->eloquentRelationships()->get($field->handle());
Expand Down
10 changes: 9 additions & 1 deletion src/Traits/HasRunwayResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,15 @@ public function publish($options = [])
if ($this->revisionsEnabled()) {
$model = $this->publishWorkingCopy($options);

Relationships::for($model)->with($model->runwayRelationships)->save();
// if there are non-revisable HasMany fields, don't save them
$except = $model->runwayResource()->blueprint()->fields()->all()
->reject(fn (Field $field) => $field->isRevisable())
->filter(fn (Field $field) => $field->fieldtype() instanceof HasManyFieldtype)
->map(fn (Field $field) => $field->handle())
->keys()
->all();

Relationships::for($model)->with($model->runwayRelationships)->except($except)->save();

return $model;
}
Expand Down

0 comments on commit eba51da

Please sign in to comment.