Skip to content

Commit

Permalink
Small doc change
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Feb 16, 2024
1 parent 1bbfec6 commit b53f2b0
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions docs/as-a-resource/from-data-to-array.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ title: From data to array
weight: 1
---

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

```php
SongData::from(Song::first())->toJson();
SongData::from(Song::first())->toArray();
```

Or transform a data object to an array:
Which will output the following array:

```php
SongData::from(Song::first())->toArray();
[
'name' => 'Never gonna give you up',
'artist' => 'Rick Astley'
]
```

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.
Expand All @@ -23,6 +26,12 @@ If you only want to transform a data object to an array without transforming the
SongData::from(Song::first())->all();
```

You can also manually transform a data object to JSON:

```php
SongData::from(Song::first())->toJson();
```

## Using collections

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

0 comments on commit b53f2b0

Please sign in to comment.