- What is jolicode/automapper and how does it work in a Laravel app?
- It is a framework-agnostic PHP object-to-object mapper that uses PHP attributes to define mappings and generates mapper code on the fly for speed. In Laravel you install it via Composer, define DTOs and mapping attributes, and bind the mapper as a singleton in a service provider. It fits action and service layers and API payload mapping, but it is not Eloquent-aware, so keep model mapping explicit.
- How do I install and configure jolicode/automapper in Laravel?
- Install it with Composer and register a singleton binding for the mapper in a service provider, pointing it at your source and target namespaces. Define DTOs with mapping attributes or mapper profiles, then resolve the mapper from the container in services or actions. You should also plan a generated-code cache directory and a warmup step for deployments.
- Which PHP and Laravel versions does jolicode/automapper support?
- It is framework-agnostic and is best suited to Laravel 10, 11, or 12-era applications running PHP 8.1 or newer. Because the repository metadata is marked unknown, verify the exact PHP and dependency constraints in Composer before adopting it. Check for conflicts with existing packages and test generated-code behavior on your target PHP version.
- How do I map nested objects and collections with jolicode/automapper?
- Attribute-driven mappings handle nested DTOs and collections automatically when the target types are declared clearly. Define each nested DTO with its own mapping attributes so the generator can build the full mapping graph. Keep mapping boundaries explicit for arrays and collections, and add contract tests to confirm nested output matches expectations.
- Does jolicode/automapper work with Eloquent models?
- It is not Eloquent-aware, so it does not understand relations, lazy loading, casts, or circular references automatically. You can still map Eloquent models, but do it deliberately by loading required relations up front to avoid N+1 queries and mapping to DTOs at a clear boundary. Avoid exposing models directly and keep field mappings explicit to prevent unintended data leakage.
- How is generated mapper code cached and warmed during deployment?
- The library generates mapper code for speed, so production should keep that generation out of the request path. Warm the generated code during deployment or in CI, and make sure every application server shares a consistent cache build with correct permissions. Missing or unwritable cache directories and stale mappers after DTO changes are common failure modes, so include warmup in your release checklist.
- How do I diagnose mapping errors in jolicode/automapper?
- Mapping exceptions typically point to type mismatches, missing attributes, or unmapped properties, so start by checking the source and target definitions. Add focused tests that map representative payloads and compare the output to expected DTOs. Keep mapping configuration small and explicit per bounded context so failures are easier to isolate.
- Can I use jolicode/automapper with enums, dates, and custom value formats like money or IDs?
- The mapper is designed for object-to-object mapping and supports custom transformers for formats such as money, IDs, and API-specific values. Enum and date handling depend on the declared target types and any transformers you register, so verify them with contract tests. Document the transformers your team uses so mapping behavior stays predictable.
- Is jolicode/automapper a replacement for Laravel API Resources?
- No, it complements Laravel API Resources rather than replacing them. Use the mapper for object-to-object mapping between DTOs, domain objects, and integration payloads, and keep API Resources for HTTP presentation where they fit. This separation keeps HTTP formatting concerns distinct from mapping logic.
- Is jolicode/automapper safe to use in production and how do I manage maintenance risk?
- The MIT license is favorable, but the package has a small ecosystem and unknown repository metadata, so pin the version and monitor releases. Own integration tests internally and assign a clear maintainer for mapping conventions and cache strategy. Keep manual mappers or Laravel Resources available as a fallback for critical paths and roll back if needed.