Skip to content

Commit

Permalink
Add extra docs
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Jul 5, 2023
1 parent 877051d commit a317c16
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
10 changes: 9 additions & 1 deletion docs/as-a-resource/wrapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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);
```
1 change: 1 addition & 0 deletions tests/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.'],
]);

0 comments on commit a317c16

Please sign in to comment.