You are viewing the documentation for an older version of this package. You can check the version you are using with the following command:
composer show spatie/laravel-data
Default values
There are a few ways to define default values for a data object. Since a data object is just a regular PHP class, you can use the constructor to set default values:
classSongDataextendsData
{
publicfunction__construct(publicstring$title = 'Never Gonna Give You Up',
publicstring$artist = 'Rick Astley',
) {
}
}
This works for simple types like strings, integers, floats, booleans, enums and arrays. But what if you want to set a default value for a more complex type like a CarbonImmutable object? You can use the constructor to do this: