From a317c16ae19aee95295840449576191744db95c6 Mon Sep 17 00:00:00 2001 From: Ruben Van Assche Date: Wed, 5 Jul 2023 10:40:05 +0200 Subject: [PATCH] Add extra docs --- docs/as-a-resource/wrapping.md | 10 +++++++++- tests/DataTest.php | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/docs/as-a-resource/wrapping.md b/docs/as-a-resource/wrapping.md index 29b27a5f..1645e08c 100644 --- a/docs/as-a-resource/wrapping.md +++ b/docs/as-a-resource/wrapping.md @@ -29,7 +29,7 @@ Now the JSON looks like this: } ``` -Data objects will only get wrapped when you're sending them as a response and never when calling `toArray` or `toJson` on it. +Data objects and collections will only get wrapped when you're sending them as a response and never when calling `toArray` or `toJson` on it. It is possible to define a default wrap key inside a data object: @@ -212,3 +212,11 @@ Whenever a data object is wrapped due to the default wrap method or a global wra ```php SongData::from(Song::first())->withoutWrapping(); ``` + +## Getting a wrapped array + +By default, `toArray` and `toJson` will never wrap a data object or collection, but it is possible to get a wrapped array: + +```php +SongData::from(Song::first())->wrap('data')->transform(wrapExecutionType: WrapExecutionType::Enabled); +``` diff --git a/tests/DataTest.php b/tests/DataTest.php index d4f5ecca..0319fd62 100644 --- a/tests/DataTest.php +++ b/tests/DataTest.php @@ -2403,3 +2403,4 @@ public function __construct( yield 'no params' => [[], 'Could not create `Spatie\LaravelData\Tests\Fakes\MultiData`: the constructor requires 2 parameters, 0 given. Parameters missing: first, second.'], yield 'one param' => [['first' => 'First'], 'Could not create `Spatie\LaravelData\Tests\Fakes\MultiData`: the constructor requires 2 parameters, 1 given. Parameters given: first. Parameters missing: second.'], ]); +