Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: ordering of multiple images with relationship #213

Merged
merged 1 commit into from
Jun 24, 2023
Merged
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
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ CuratorPicker::make(string $fieldName)
->imageResizeTargetHeight()
->multiple() // required if using a relationship with multiple media
->relationship(string $relationshipName, string 'titleColumnName')
->orderColumn('order') // only necessary to rename the order column if using a relationship with multiple media
```

### Relationships
Expand Down Expand Up @@ -150,7 +151,8 @@ Form component
```php
CuratorPicker::make('product_picture_ids')
->multiple()
->relationship('product_pictures', 'id'),
->relationship('product_pictures', 'id')
->orderColumn('order'), // only necessary if you need to rename the order column
```

Model
Expand All @@ -160,7 +162,10 @@ use Awcodes\Curator\Models\Media;

public function productPictures(): BelongsTo
{
return $this->belongsToMany(Media::class, 'media_post', 'post_id', 'media_id');
return $this
->belongsToMany(Media::class, 'media_post', 'post_id', 'media_id')
->withPivot('order')
->orderBy('order');
}
```

Expand Down
33 changes: 31 additions & 2 deletions src/Components/Forms/CuratorPicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class CuratorPicker extends Field

protected string|Closure|null $relationship = null;

protected string | null $orderColumn = null;

/**
* @throws Exception
*/
Expand Down Expand Up @@ -244,6 +246,13 @@ public function preserveFilenames(bool|Closure|null $condition = true): static
return $this;
}

public function orderColumn(string $column): static
{
$this->orderColumn = $column;

return $this;
}

/**
* @deprecated
*/
Expand Down Expand Up @@ -341,6 +350,11 @@ public function isMultiple(): bool
return $this->evaluate($this->isMultiple);
}

public function getOrderColumn(): string
{
return $this->orderColumn ?? 'order';
}

public function relationship(string | Closure $relationshipName, string | Closure $titleColumnName, ?Closure $callback = null): static
{
$this->relationship = $relationshipName;
Expand Down Expand Up @@ -380,14 +394,29 @@ public function relationship(string | Closure $relationshipName, string | Closur
return;
}

$relationship = $component->getRelationship();

if ($component->isMultiple()) {
if (
($relationship instanceof BelongsToMany) &&
in_array($component->getOrderColumn(), $relationship->getPivotColumns())
) {
$orderColumn = $component->getOrderColumn();
$state = collect(array_values($state))->mapWithKeys(function($item, $index) use ($orderColumn) {
return [$item['id'] => [$orderColumn => $index + 1]];
});

$relationship->sync($state ?? []);
return;
}

$state = Arr::pluck($state,'id');
$component->getRelationship()->sync($state ?? []);
$relationship->sync($state ?? []);

return;
}

$component->getRelationship()->associate(Arr::first($state)['id']);
$relationship->associate(Arr::first($state)['id']);
$record->save();
});

Expand Down