Skip to content

Commit b53f2b0

Browse files
Small doc change
1 parent 1bbfec6 commit b53f2b0

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

docs/as-a-resource/from-data-to-array.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@ title: From data to array
33
weight: 1
44
---
55

6-
You can manually transform a data object to JSON:
6+
A data object can automatically be transformed into an array as such:
77

88
```php
9-
SongData::from(Song::first())->toJson();
9+
SongData::from(Song::first())->toArray();
1010
```
1111

12-
Or transform a data object to an array:
12+
Which will output the following array:
1313

1414
```php
15-
SongData::from(Song::first())->toArray();
15+
[
16+
'name' => 'Never gonna give you up',
17+
'artist' => 'Rick Astley'
18+
]
1619
```
1720

1821
By default, calling `toArray` on a data object will recursively transform all properties to an array. This means that nested data objects and collections of data objects will also be transformed to arrays. Other complex types like `Carbon`, `DateTime`, `Enums`, etc... will be transformed into a string. We'll see in the [transformers](/docs/laravel-data/v4/as-a-resource/transformers) section how to configure and customize this behavior.
@@ -23,6 +26,12 @@ If you only want to transform a data object to an array without transforming the
2326
SongData::from(Song::first())->all();
2427
```
2528

29+
You can also manually transform a data object to JSON:
30+
31+
```php
32+
SongData::from(Song::first())->toJson();
33+
```
34+
2635
## Using collections
2736

2837
Here's how to create a collection of data objects:

0 commit comments

Comments
 (0)