Search code examples
phplaravellaravel-data

spatie laravel data transfer object array of string


Using laravel data to create data transfer objects. I need to declare a property that takes an array of strings.

The post payload will be as follows:

{
    "countries": ["en", "de"]
}

How do I declare this in the laravel data object. Tried doing this

class ExampleData extends Data
{
public function __construct(

    public ?string $someProperty,

    #[DataCollectionOf(string::class)]
    public ?DataCollection $countries) 
    {
    }
}

However this throws an error Data collection property countries has an annotation that isn't a data object or is missing an annotation. How can I declare an array of integers or strings in this data object.


Solution

  • The right way should be to declare it with an array keyword

    public ?array $countries 
    

    The above line works