spatie laravel data transfer object array of string

114 Views Asked by At

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.

1

There are 1 best solutions below

0
user275157 On BEST ANSWER

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

public ?array $countries 

The above line works