shureban/laravel-object-mapper
Laravel object mapper maps JSON strings, arrays, and FormRequest data into typed PHP objects (DTOs) using reflection. Use inheritance, a trait, or ObjectMapper directly; supports readonly DTOs and static constructors. Works out of the box via auto-discovery.
Install via Composer:
composer require shureban/laravel-object-mapper
Publish the config (optional):
php artisan vendor:publish --provider="Shureban\ObjectMapper\ObjectMapperServiceProvider"
Version note (v2.0.1): The current line is 2.x, which adds Laravel 13 support. If you are on an older Laravel release, check the package's compatibility matrix before upgrading — 1.x and 2.x are separate major lines, so pin your constraint deliberately (^2.0 vs ^1.2) rather than using *.
First Use Case: Transforming arrays to objects with automatic snake_case-to-camelCase conversion:
use Shureban\ObjectMapper\Facades\ObjectMapper;
$data = ['user_name' => 'john_doe', 'user_age' => 30];
$user = ObjectMapper::map($data, \App\Models\User::class);
// $user now has properties: userName, userAge
$object = ObjectMapper::map($arrayData, $targetClass);
$complexObject = ObjectMapper::map($nestedArray, \App\Models\Order::class);
// Automatically handles nested snake_case keys
// config/object-mapper.php
'mappers' => [
'App\Models\User' => \App\Mappers\UserMapper::class,
],
FormRequest to auto-map validated input:
public function rules() { /* ... */ }
public function map(array $data) {
return ObjectMapper::map($data, \App\Models\User::class);
}
public function toArray($request) {
return ObjectMapper::mapCollection($this->collection, \App\DTO\UserDTO::class);
}
2.x: Patch releases in this line are compatibility-focused (v2.0.1 is purely Laravel 13 support), so they are low-risk. Treat the 1.x → 2.x jump as the real migration boundary — run your mapper test suite and check custom TransformerInterface implementations against the v2 API before switching.^2.0 is safe for day-to-day work.1.x installs are not covered by the Laravel 13 support commit.user_name → userName).
protected $fillable = ['user_name'], ensure your setter methods match the auto-converted names (e.g., setUserName()).class or static may cause issues. Rename them in your source data.true/false). Override with explicit type hints in your model/DTO.composer why shureban/laravel-object-mapper first when mappings start behaving unexpectedly after an upgrade.ObjectMapper::setDebug(true); // Logs transformations to storage/logs/object-mapper.log
$mappedData = ObjectMapper::getMappedData(); // Returns the last transformation result
\Shureban\ObjectMapper\Transformers\TransformerInterface for complex logic.mapped and mapping events in your EventServiceProvider:
protected $listen = [
'Shureban\ObjectMapper\Events\Mapping' => [
\App\Listeners\LogMapping::class,
],
];
How can I help you explore Laravel packages today?