- How do I install AutoMapper+ in a Laravel project?
- Run `composer require mark-gerarts/auto-mapper-plus` in your project root. No additional Laravel-specific setup is required, though you can bind it to the service container via `app()->bind()` for dependency injection. Ensure your PHP version is 8.1+ for full compatibility.
- Can AutoMapper+ handle Laravel Eloquent models with private properties?
- Yes, AutoMapper+ can map private properties without reflection hacks or manual getters/setters. It uses PHP’s native reflection capabilities to access protected/private properties, making it seamless for Eloquent models. No extra configuration is needed for this.
- Does AutoMapper+ support Laravel’s API resources (e.g., Spatie Laravel API Resources)?
- AutoMapper+ can pre-process data before serialization by API resources. Use it to transform Eloquent models into DTOs, then pass those DTOs to your API resources. This reduces duplication and keeps your response logic centralized in the mapper.
- What Laravel versions does AutoMapper+ officially support?
- AutoMapper+ is framework-agnostic but works with Laravel 9+ (PHP 8.1+). It integrates with Laravel’s service container and DI system, so it’s fully compatible with modern Laravel apps. Tested with Laravel 10+ in production environments.
- How do I handle nested object mappings in AutoMapper+?
- AutoMapper+ supports nested mappings out of the box. Define mappings for parent and child objects separately, and the mapper will recursively handle nested structures. For example, map `User` to `UserDto` with an embedded `AddressDto` without manual recursion.
- Is AutoMapper+ performant enough for high-traffic Laravel APIs?
- AutoMapper+ uses reflection, which can introduce latency in high-throughput APIs. Benchmark critical paths with tools like Blackfire or Xdebug. For performance-critical apps, consider caching compiled mappings or pre-mapping data during off-peak hours.
- How do I test AutoMapper+ mappings in Laravel?
- Use PHPUnit or Pest to test mappings by asserting the output of `mapper->map()` calls. Mock dependencies with Mockery if needed. For edge cases, use property-based testing (e.g., with Pest’s `with()`) to validate mappings against various input types.
- Can I use AutoMapper+ with Laravel’s Query Builder or raw SQL results?
- Yes, AutoMapper+ can transform raw query results (e.g., `DB::select()`) into structured objects or arrays. Define mappings for `stdClass` or arrays to DTOs, and use it to standardize API responses or internal data formats.
- What alternatives to AutoMapper+ exist for Laravel?
- Alternatives include `spatie/laravel-data` (for immutable DTOs), `laravel-shift/doctrine-types` (for Doctrine integration), or manual mapping with Laravel’s `collect()` methods. AutoMapper+ stands out for its flexibility with custom callbacks and nested mappings.
- How do I debug or log mapping failures in production?
- Wrap `mapper->map()` calls in try-catch blocks to log exceptions via Laravel’s `Log` facade. For debugging, enable PHP error logging and use Xdebug to inspect the mapping process. Consider adding validation (e.g., `ValidatesWhenResolved`) to catch issues early.