- How do I install skraeda/laravel-automapper?
- Install the package with Composer by running composer require skraeda/laravel-automapper in your Laravel project. Laravel package discovery will register the service provider, and you can resolve the mapper from the container. Check the README for any optional configuration or publishing steps before defining your first mapping.
- What is the difference between Laravel AutoMapper and AutoMapper+?
- Laravel AutoMapper is a Laravel integration for Mark Gerarts' AutoMapper+ library, so it provides the Laravel wiring around the same mapping engine. AutoMapper+ itself is framework-agnostic and can be used in any PHP project, while this package adds Laravel service container integration and conveniences for request data and Eloquent models. Use the standalone library outside Laravel, and use this package when you want DTO mapping that fits Laravel conventions.
- How do I map an Eloquent model to a DTO with Laravel AutoMapper?
- Define a typed DTO class with public properties for the fields you need, then map the Eloquent model to that class with the mapper's map method. For example, resolve the mapper and call map($user, UserDto::class) to produce a DTO from a User model. Configure property names or nested types where the model attributes and DTO properties do not match exactly.
- Does Laravel AutoMapper support nested DTOs and nested arrays?
- Yes. Laravel AutoMapper supports nested mapping, so a source array or object can be converted into a nested DTO when the target type is declared in the mapping configuration or attributes. The mapper recurses through registered mappings, which lets you map relationships and nested request payloads without manually instantiating every child object. Keep nested mappings explicit so the target classes and transformations remain easy to test.
- How do I map Laravel request data into a DTO?
- Use a FormRequest or validator to validate the incoming payload, then pass only the validated array to the mapper and target your DTO class. A typical flow is $dto = $mapper->map($request->validated(), CreateOrderDto::class). Avoid mapping the entire request object so unvalidated fields cannot leak into the DTO.
- Does Laravel AutoMapper automatically cast property types?
- The package provides automatic property casting when source values differ from the DTO's typed properties, such as converting strings to integers or booleans. Date and nested object casting depend on your mapping configuration and the target property type. Casting is not a substitute for validation, so validate input first and treat mapping failures as application errors.
- How do I configure mappings with attributes in Laravel AutoMapper?
- Attribute-based configuration lets you keep mapping rules close to the DTO by declaring source keys, nested target types, or transformations directly on the class or its properties. For more complex cases, register explicit mapping definitions or callbacks in a service provider so the behavior is centralized. The attributes reduce boilerplate for common mappings while leaving advanced transformations explicit.
- Which Laravel and PHP versions does skraeda/laravel-automapper support?
- The exact Laravel and PHP version constraints are defined in the package's composer.json, so check the repository and Packagist metadata before installing. Because it wraps AutoMapper+, the PHP requirement must also satisfy that library and your Laravel release. If you are on an older Laravel version, confirm compatibility or use a tagged release that matches your stack.
- How do I test DTO mappings in a Laravel application?
- Mappings are plain PHP classes and arrays, so you can unit test them by resolving the mapper from Laravel's container and asserting the resulting DTO properties. Keep mapping definitions in a service provider so tests exercise the same configuration used in production. For feature tests, submit request data through the route and assert the DTO or persisted model that results.
- Is skraeda/laravel-automapper production-ready and actively maintained?
- The package is intended for production Laravel applications, but you should review recent commits, releases, and issue activity to judge its maintenance for your version of Laravel. Alternatives include manual mapping, constructor hydration after validation, Spatie's laravel-data package, or the standalone AutoMapper+ library. Choose Laravel AutoMapper when you want AutoMapper+ mapping definitions and attribute-based DTO mapping with Laravel integration.