> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/spatie/laravel-data/llms.txt
> Use this file to discover all available pages before exploring further.

# From Data to Array

> Transform Laravel Data objects into arrays and JSON

A data object can automatically be transformed into an array:

```php theme={null}
SongData::from(Song::first())->toArray();
```

Which outputs:

```php theme={null}
[
    'name' => 'Never gonna give you up',
    'artist' => 'Rick Astley'
]
```

<Note>
  By default, `toArray` recursively transforms all properties. Nested data objects, collections, and complex types like `Carbon`, `DateTime`, `Enums` will be transformed.
</Note>

## Without Transforming Properties

If you only want to transform a data object to an array without transforming the properties:

```php theme={null}
SongData::from(Song::first())->all();
```

## To JSON

You can also manually transform a data object to JSON:

```php theme={null}
SongData::from(Song::first())->toJson();
```

## Using Collections

Create a collection of data objects:

```php theme={null}
SongData::collect(Song::all());
```

Transform the collection to an array:

```php theme={null}
SongData::collect(Song::all())->toArray();
```

Which outputs:

```php theme={null}
[
    [
        "name" => "Never Gonna Give You Up",
        "artist" => "Rick Astley"
    ],
    [
        "name" => "Giving Up on Love",
        "artist" => "Rick Astley"
    ]
]
```

## Nesting Data Objects

You can nest data objects within other data objects:

```php theme={null}
class UserData extends Data
{
    public function __construct(
        public string $title,
        public string $email,
        public SongData $favorite_song,
    ) {
    }
    
    public static function fromModel(User $user): self
    {
        return new self(
            $user->title,
            $user->email,
            SongData::from($user->favorite_song)
        );
    }
}
```

When transformed to an array:

```php theme={null}
[
    "name" => "Ruben",
    "email" => "ruben@spatie.be",
    "favorite_song" => [
        "name" => "Never Gonna Give You Up",
        "artist" => "Rick Astley"
    ]
]
```

## Nesting Collections

You can also nest a collection of data objects:

```php theme={null}
class AlbumData extends Data
{
    /**
    * @param Collection<int, SongData> $songs
    */
    public function __construct(
        public string $title,
        public array $songs,
    ) {
    }

    public static function fromModel(Album $album): self
    {
        return new self(
            $album->title,
            SongData::collect($album->songs)
        );
    }
}
```

<Warning>
  Always type collections of data objects by annotation or the `DataCollectionOf` attribute. This is essential for correct transformation.
</Warning>

## Hiding properties

You can hide specific properties from being included in the transformation using the `Hidden` attribute:

```php theme={null}
use Spatie\LaravelData\Attributes\Hidden;
use Spatie\LaravelData\Data;

class UserData extends Data
{
    public function __construct(
        public string $name,
        public string $email,
        #[Hidden]
        public string $password,
    ) {
    }
}
```

The `password` property will never be included when transforming the data object to an array or JSON:

```php theme={null}
UserData::from([
    'name' => 'John Doe',
    'email' => 'john@example.com',
    'password' => 'secret',
])->toArray();

// Result:
[
    'name' => 'John Doe',
    'email' => 'john@example.com',
    // password is not included
]
```

<Note>
  The `Hidden` attribute is useful for excluding sensitive data like passwords, API tokens, or internal properties from API responses.
</Note>
