diff --git a/src/Entries/Entry.php b/src/Entries/Entry.php index fd76d84dd16..7efca7bfab4 100644 --- a/src/Entries/Entry.php +++ b/src/Entries/Entry.php @@ -9,6 +9,7 @@ use Illuminate\Contracts\Support\Responsable; use Illuminate\Support\Carbon; use Illuminate\Support\Traits\Localizable; +use JsonSerializable; use LogicException; use Statamic\Contracts\Auth\Protect\Protectable; use Statamic\Contracts\Data\Augmentable; @@ -52,7 +53,7 @@ use Statamic\Support\Str; use Statamic\Support\Traits\FluentlyGetsAndSets; -class Entry implements Arrayable, ArrayAccess, Augmentable, ContainsQueryableValues, Contract, Localization, Protectable, ResolvesValuesContract, Responsable, SearchableContract +class Entry implements Arrayable, ArrayAccess, Augmentable, ContainsQueryableValues, Contract, JsonSerializable, Localization, Protectable, ResolvesValuesContract, Responsable, SearchableContract { use ContainsComputedData, ContainsData, ExistsAsFile, FluentlyGetsAndSets, HasAugmentedInstance, Localizable, Publishable, Revisable, Searchable, TracksLastModified, TracksQueriedColumns, TracksQueriedRelations; @@ -999,4 +1000,9 @@ protected function getComputedCallbacks() { return Facades\Collection::getComputedCallbacks($this->collection); } + + public function jsonSerialize(): mixed + { + return $this->toArray(); + } } diff --git a/tests/Data/Entries/EntryTest.php b/tests/Data/Entries/EntryTest.php index 2f00200244c..fa93368a9a9 100644 --- a/tests/Data/Entries/EntryTest.php +++ b/tests/Data/Entries/EntryTest.php @@ -2438,4 +2438,19 @@ public function initially_saved_entry_gets_put_into_events() ['7', '7'], ], $events->map(fn ($event) => [$event->entry->id(), $event->initiator->id()])->all()); } + + /** @test */ + public function entry_is_json_serializable() + { + $entry = EntryFactory::collection('test')->create(); + $entry->set('title', 'Serializable Title'); + + // Json serialize and deserialize + $json = json_encode($entry); + $array = json_decode($json, true); + + $this->assertJson($json); + $this->assertArrayHasKey('title', $array); + $this->assertTrue(data_get($array, 'collection.handle') === 'test'); + } }