diff --git a/resources/views/fields/file-option.blade.php b/resources/views/fields/file-option.blade.php
index edabacad..7766394c 100644
--- a/resources/views/fields/file-option.blade.php
+++ b/resources/views/fields/file-option.blade.php
@@ -1,17 +1,13 @@
- @if($medium->isImage)
-
![{{ $label }}]({{ $medium->getUrl('thumbnail') }})
+ @if($isImage)
+
![{{ $label }}]({{ $url }})
@else
@endif
-
{{ $label }}
+
{{ $label }}
@@ -20,8 +16,8 @@ class="file-list-item__thumbnail"
type="button"
class="btn btn--light btn--sm btn--icon"
aria-label="{{ __('Edit') }}"
- aria-describedby="{{ $medium->uuid }}"
- x-on:click="$dispatch('open-{{ $medium->uuid }}')"
+ aria-describedby="{{ $uuid }}"
+ x-on:click="$dispatch('open-{{ $uuid }}')"
>
@@ -30,7 +26,7 @@ class="btn btn--light btn--sm btn--icon"
type="button"
class="btn btn--delete btn--sm btn--icon"
aria-label="{{ __('Remove') }}"
- aria-describedby="{{ $medium->uuid }}"
+ aria-describedby="{{ $uuid }}"
x-on:click="selection.splice(index, 1)"
>
@@ -39,7 +35,7 @@ class="btn btn--delete btn--sm btn--icon"
@if(! empty($fields))
-
+
@foreach($fields as $field)
@include($field['template'], $field)
@endforeach
diff --git a/resources/views/media/medium.blade.php b/resources/views/media/medium.blade.php
index 2d0ce59e..361ce6b6 100644
--- a/resources/views/media/medium.blade.php
+++ b/resources/views/media/medium.blade.php
@@ -16,10 +16,10 @@ class="media-item"
-
+
-
+
diff --git a/src/Console/Commands/ResourceMake.php b/src/Console/Commands/ResourceMake.php
index 2ac24db8..ff3f6da5 100644
--- a/src/Console/Commands/ResourceMake.php
+++ b/src/Console/Commands/ResourceMake.php
@@ -59,7 +59,7 @@ protected function buildClass($name): string
}
/**
- * Create a new method.
+ * Replace the model class.
*/
public function replaceModel(string $class): string
{
diff --git a/src/Fields/BelongsToMany.php b/src/Fields/BelongsToMany.php
index 7b64c870..a254057e 100644
--- a/src/Fields/BelongsToMany.php
+++ b/src/Fields/BelongsToMany.php
@@ -18,6 +18,11 @@ class BelongsToMany extends Relation
*/
protected ?Closure $pivotFieldsResolver = null;
+ /**
+ * The default pivot values that should be saved.
+ */
+ protected array $pivotValues = [];
+
/**
* Create a new relation field instance.
*/
@@ -87,28 +92,15 @@ public function withPivotFields(Closure $callback): static
/**
* {@inheritdoc}
*/
- public function toOption(Request $request, Model $model, Model $related): array
+ public function getValueForHydrate(Request $request): mixed
{
- $relation = $this->getRelation($model);
-
- if (! $related->relationLoaded($relation->getPivotAccessor())) {
- $related->setRelation($relation->getPivotAccessor(), $relation->newPivot());
- }
-
- $option = parent::toOption($request, $model, $related);
-
- $option['attrs']['name'] = sprintf(
- '%s[%s][%s]',
- $this->getAttribute('name'),
- $related->getKey(),
- $this->getRelation($model)->getRelatedPivotKeyName()
- );
+ $value = (array) parent::getValueForHydrate($request);
- $option['fields'] = is_null($this->pivotFieldsResolver)
- ? new Fields()
- : call_user_func_array($this->pivotFieldsResolver, [$request, $model, $related]);
+ $value = Arr::isList($value) ? array_fill_keys($value, []) : $value;
- return $option;
+ return array_map(function (array $pivot): array {
+ return array_merge($this->pivotValues, $pivot);
+ }, $value);
}
/**
@@ -130,15 +122,11 @@ public function resolveHydrate(Request $request, Model $model, mixed $value): vo
{
if (is_null($this->hydrateResolver)) {
$this->hydrateResolver = function (Request $request, Model $model, mixed $value): void {
- $value = (array) $value;
-
- $value = Arr::isList($value) ? array_fill_keys($value, []) : $value;
-
$relation = $this->getRelation($model);
$results = $this->resolveRelatableQuery($request, $model)
->findMany(array_keys($value))
- ->each(static function (Model $related) use ($relation, $value): void {
+ ->each(function (Model $related) use ($relation, $value): void {
$related->setRelation(
$relation->getPivotAccessor(),
$relation->newPivot($value[$related->getKey()])
@@ -155,27 +143,38 @@ public function resolveHydrate(Request $request, Model $model, mixed $value): vo
/**
* {@inheritdoc}
*/
- public function toArray(): array
+ public function toOption(Request $request, Model $model, Model $related): array
{
- return array_merge(parent::toArray(), [
- 'relatedName' => $this->getRelatedName(),
- ]);
+ $relation = $this->getRelation($model);
+
+ if (! $related->relationLoaded($relation->getPivotAccessor())) {
+ $related->setRelation($relation->getPivotAccessor(), $relation->newPivot());
+ }
+
+ $option = parent::toOption($request, $model, $related);
+
+ $option['attrs']['name'] = sprintf(
+ '%s[%s][%s]',
+ $this->getAttribute('name'),
+ $related->getKey(),
+ $this->getRelation($model)->getRelatedPivotKeyName()
+ );
+
+ $option['fields'] = is_null($this->pivotFieldsResolver)
+ ? []
+ : call_user_func_array($this->pivotFieldsResolver, [$request, $model, $related])->mapToFormComponents($request, $model);
+
+ return $option;
}
/**
- * Create a new method.
+ * {@inheritdoc}
*/
- public function toFormComponent(Request $request, Model $model): array
+ public function toArray(): array
{
- $data = parent::toFormComponent($request, $model);
-
- $data['options'] = array_map(static function (array $option) use ($request, $model): array {
- return array_merge($option, [
- 'fields' => $option['fields']->mapToFormComponents($request, $model),
- ]);
- }, $data['options']);
-
- return $data;
+ return array_merge(parent::toArray(), [
+ 'relatedName' => $this->getRelatedName(),
+ ]);
}
/**
diff --git a/src/Fields/Boolean.php b/src/Fields/Boolean.php
index 91ceac61..64637668 100644
--- a/src/Fields/Boolean.php
+++ b/src/Fields/Boolean.php
@@ -41,7 +41,7 @@ public function checked(bool $value = true): static
}
/**
- * Create a new method.
+ * {@inheritdoc}
*/
public function resolveValue(Request $request, Model $model): mixed
{
diff --git a/src/Fields/File.php b/src/Fields/File.php
index 78671034..1b68aefe 100644
--- a/src/Fields/File.php
+++ b/src/Fields/File.php
@@ -78,6 +78,16 @@ public function disk(string $value): static
return $this;
}
+ /**
+ * Set the collection pivot value.
+ */
+ public function collection(string $value): static
+ {
+ $this->pivotValues['collection'] = $value;
+
+ return $this;
+ }
+
/**
* {@inheritdoc}
*/
@@ -210,7 +220,7 @@ protected function prune(Request $request, Model $model, array $keys): int
}
/**
- * Create a new method.
+ * {@inheritdoc}
*/
public function toOption(Request $request, Model $model, Model $related): array
{
@@ -226,11 +236,10 @@ public function toOption(Request $request, Model $model, Model $related): array
$option['attrs']->merge(['name' => $name]);
return array_merge($option, [
- 'file_name' => $related->file_name,
- 'is_image' => $related->isImage,
- 'medium' => $related,
+ 'fileName' => $related->file_name,
+ 'isImage' => $related->isImage,
'processing' => false,
- 'url' => $related->getUrl('thumbnail') ?: $related->getUrl('original'),
+ 'url' => $related->hasConversion('thumbnail') ? $related->getUrl('thumbnail') : $related->getUrl(),
'uuid' => $related->uuid,
]);
}
diff --git a/src/Fields/Media.php b/src/Fields/Media.php
index 29548442..b15ac227 100644
--- a/src/Fields/Media.php
+++ b/src/Fields/Media.php
@@ -90,8 +90,6 @@ public function paginate(Request $request, Model $model): array
->through(function (Medium $related) use ($request, $model): array {
$option = $this->toOption($request, $model, $related);
- $option['fields'] = $option['fields']->mapToFormComponents($request, $model);
-
return array_merge($option, [
'html' => View::make('root::fields.file-option', $option)->render(),
]);
@@ -146,7 +144,7 @@ public function store(Request $request, Model $model, UploadedFile $file): array
if ($request->header('X-Chunk-Index') !== $request->header('X-Chunk-Total')) {
return array_merge($this->toOption($request, $model, new Medium()), [
'processing' => true,
- 'file_name' => null,
+ 'fileName' => null,
]);
}
@@ -199,9 +197,7 @@ public function toFormComponent(Request $request, Model $model): array
'multiple' => $this->multiple,
'chunk_size' => Config::get('root.media.chunk_size'),
],
- 'selection' => array_map(static function (array $option) use ($request, $model): array {
- $option['fields'] = $option['fields']->mapToFormComponents($request, $model);
-
+ 'selection' => array_map(static function (array $option): array {
return array_merge($option, [
'html' => View::make('root::fields.file-option', $option)->render(),
]);
diff --git a/src/Models/Attachment.php b/src/Models/Attachment.php
index da7fd045..297816f0 100644
--- a/src/Models/Attachment.php
+++ b/src/Models/Attachment.php
@@ -6,10 +6,6 @@
class Attachment extends MorphPivot
{
- protected $attributes = [
- 'collection' => 'foo',
- ];
-
/**
* The attributes that should be cast to native types.
*
@@ -18,4 +14,14 @@ class Attachment extends MorphPivot
protected $casts = [
'meta' => 'json',
];
+
+ /**
+ * The attributes that are mass assignable.
+ *
+ * @var array
+ */
+ protected $fillable = [
+ 'collection',
+ 'meta',
+ ];
}
diff --git a/src/Models/Medium.php b/src/Models/Medium.php
index f1f392db..096014fc 100644
--- a/src/Models/Medium.php
+++ b/src/Models/Medium.php
@@ -264,6 +264,14 @@ public function getUrl(string $conversion = null): string
return URL::to(Storage::disk($this->disk)->url($this->getPath($conversion)));
}
+ /**
+ * Check if the medium has the given conversion.
+ */
+ public function hasConversion(string $conversion): bool
+ {
+ return in_array($conversion, $this->properties['conversions'] ?? []);
+ }
+
/**
* Scope the query only to the given search term.
*/