Skip to main content
It is possible to automatically create data objects in all sorts of forms with this package. Sometimes a little bit more control is required when a data object is being created. This is where factories come in. Factories allow you to create data objects like before but allow you to customize the creation process. For example, we can create a data object using a factory like this:
Collecting a bunch of data objects using a factory can be done as such:

Disable Property Name Mapping

We saw earlier that it is possible to map property names when creating a data object from an array. This can be disabled when using a factory:

Changing the Validation Strategy

By default, the package will only validate Requests when creating a data object it is possible to change the validation strategy to always validate for each type:
Or completely disable validation:

Disabling Magic Methods

A data object can be created using magic methods, this can be disabled when using a factory:
It is also possible to ignore the magical creation methods when creating a data object as such:

Disabling Optional Values

When creating a data object that has optional properties, it is possible choose whether missing properties from the payload should be created as Optional. This can be helpful when you want to have a null value instead of an Optional object - for example, when creating the DTO from an Eloquent model with null values.
Note that when an Optional property has no default value, and is not nullable, and the payload does not contain a value for this property, the DTO will not have the property set - so accessing it can throw Typed property must not be accessed before initialization error. Therefore, it’s advisable to either set a default value or make the property nullable, when using withoutOptionalValues.

Adding Additional Global Casts

When creating a data object, it is possible to add additional casts to the data object:
These casts will not replace the other global casts defined in the data.php config file, they will though run before the other global casts. You define them just like you would define them in the config file, the first parameter is the type of the property that should be cast and the second parameter is the cast class.

Using the Creation Context

Internally the package uses a creation context to create data objects. The factory allows you to use this context manually, but when using the from method it will be used automatically. It is possible to inject the creation context into a magical method by adding it as a parameter:
You can read more about creation contexts here.