It is possible to nest multiple data objects: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.
Collections of Data Objects
What if you want to nest a collection of data objects within a data object? That’s perfectly possible, but there’s a small catch; you should always define what kind of data objects will be stored within the collection. This is really important later on to create validation rules for data objects or partially transforming data objects. There are a few different ways to define what kind of data objects will be stored within a collection. You could use an annotation, for example, which has an advantage that your IDE will have better suggestions when working with the data object. And as an extra benefit, static analyzers like PHPStan will also be able to detect errors when your code is using the wrong types.Using DocBlock Annotations
A collection of data objects defined by annotation looks like this:Using Generics
It is also possible to use generics:Laravel Collections
The same is true for Laravel collections, but be sure to use two generic parameters to describe the collection. One for the collection key type and one for the data object type:Well-Annotated Collections
If the collection is well-annotated, theData class doesn’t need to use annotations:
Using Attributes
You can also use an attribute to define the type of data objects that will be stored within a collection:This was the old way to define the type of data objects that will be stored within a collection. It is still supported, but we recommend using the annotation since static analyzers and IDEs will have better support for that.