Skip to main content
A data object can automatically be transformed into an array:
SongData::from(Song::first())->toArray();
Which outputs:
[
    'name' => 'Never gonna give you up',
    'artist' => 'Rick Astley'
]
By default, toArray recursively transforms all properties. Nested data objects, collections, and complex types like Carbon, DateTime, Enums will be transformed.

Without Transforming Properties

If you only want to transform a data object to an array without transforming the properties:
SongData::from(Song::first())->all();

To JSON

You can also manually transform a data object to JSON:
SongData::from(Song::first())->toJson();

Using Collections

Create a collection of data objects:
SongData::collect(Song::all());
Transform the collection to an array:
SongData::collect(Song::all())->toArray();
Which outputs:
[
    [
        "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:
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:
[
    "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:
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)
        );
    }
}
Always type collections of data objects by annotation or the DataCollectionOf attribute. This is essential for correct transformation.

Hiding properties

You can hide specific properties from being included in the transformation using the Hidden attribute:
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:
UserData::from([
    'name' => 'John Doe',
    'email' => 'john@example.com',
    'password' => 'secret',
])->toArray();

// Result:
[
    'name' => 'John Doe',
    'email' => 'john@example.com',
    // password is not included
]
The Hidden attribute is useful for excluding sensitive data like passwords, API tokens, or internal properties from API responses.