Skip to content
Closed
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
8 changes: 7 additions & 1 deletion src/Entries/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -999,4 +1000,9 @@ protected function getComputedCallbacks()
{
return Facades\Collection::getComputedCallbacks($this->collection);
}

public function jsonSerialize(): mixed
{
return $this->toArray();
}
}
15 changes: 15 additions & 0 deletions tests/Data/Entries/EntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}