Skip to content
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
12 changes: 11 additions & 1 deletion packages/core/src/FieldTypes/ListField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace Lunar\FieldTypes;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Arr;
use JsonSerializable;
use Lunar\Base\FieldType;
use Lunar\Exceptions\FieldTypeException;

class ListField implements FieldType, JsonSerializable
class ListField implements Arrayable, FieldType, JsonSerializable
{
/**
* @var array
Expand Down Expand Up @@ -68,4 +70,12 @@ public function getConfig(): array
],
];
}

/**
* Return the value as an array (implements Arrayable for Filament 4 compatibility).
*/
public function toArray(): array
{
return Arr::wrap((array) json_decode($this->value ?? '[]', associative: true));
}
Comment thread
ryanmitchell marked this conversation as resolved.
}
12 changes: 12 additions & 0 deletions tests/core/Unit/FieldTypes/ListFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,15 @@

new ListField('Not an array');
});

test('toArray returns associative array for keyed values', function () {
$field = new ListField([
'foo' => 'bar',
'baz' => 'qux',
]);

expect($field->toArray())->toBe([
'foo' => 'bar',
'baz' => 'qux',
]);
});
Loading