PHPackages                             skraeda/laravel-automapper - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. skraeda/laravel-automapper

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

skraeda/laravel-automapper
==========================

Laravel wrapper for automapper-plus by Mark Gerarts

v3.0.2(5mo ago)710.7k↓27.3%2[3 PRs](https://github.com/Skraeda/laravel-automapper/pulls)MITPHPPHP ^8.2

Since Feb 28Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/Skraeda/laravel-automapper)[ Packagist](https://packagist.org/packages/skraeda/laravel-automapper)[ RSS](/packages/skraeda-laravel-automapper/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (7)Versions (14)Used By (0)

Laravel AutoMapper
==================

[](#laravel-automapper)

[![CircleCI](https://camo.githubusercontent.com/bc63e868deda6ba8a03044ba2d7f47a761825cc360e89b67d050580918e9acfb/68747470733a2f2f696d672e736869656c64732e696f2f636972636c6563692f6275696c642f6769746875622f536b72616564612f6c61726176656c2d6175746f6d6170706572)](https://camo.githubusercontent.com/bc63e868deda6ba8a03044ba2d7f47a761825cc360e89b67d050580918e9acfb/68747470733a2f2f696d672e736869656c64732e696f2f636972636c6563692f6275696c642f6769746875622f536b72616564612f6c61726176656c2d6175746f6d6170706572)[![Codecov](https://camo.githubusercontent.com/928c7901a11156b160cbd1b126e98cb7f1d7662bc8a4dd03c7ff5cb8a416d7d7/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f536b72616564612f6c61726176656c2d6175746f6d6170706572)](https://camo.githubusercontent.com/928c7901a11156b160cbd1b126e98cb7f1d7662bc8a4dd03c7ff5cb8a416d7d7/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f536b72616564612f6c61726176656c2d6175746f6d6170706572)[![Version Badge](https://camo.githubusercontent.com/c79cb6d2e2c84d50fe3f1a1bd4ef5464338c11f812fba48fba43bde8d772e17d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f736b72616564612f6c61726176656c2d6175746f6d6170706572)](https://camo.githubusercontent.com/c79cb6d2e2c84d50fe3f1a1bd4ef5464338c11f812fba48fba43bde8d772e17d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f736b72616564612f6c61726176656c2d6175746f6d6170706572)[![License Badge](https://camo.githubusercontent.com/9b995dcb4fa4bd003b99b883494adac9eeb319b3f725d24cfd6590f427ebbfae/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f536b72616564612f6c61726176656c2d6175746f6d61707065723f636f6c6f723d316635396334)](https://camo.githubusercontent.com/9b995dcb4fa4bd003b99b883494adac9eeb319b3f725d24cfd6590f427ebbfae/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f536b72616564612f6c61726176656c2d6175746f6d61707065723f636f6c6f723d316635396334)

This package integrates [AutoMapper+](https://github.com/mark-gerarts/automapper-plus) by Mark Gerarts into Laravel.

This documentation will assume you are familiar with how `AutoMapper+` works.

Installation
------------

[](#installation)

Install via composer:

```
composer require skraeda/laravel-automapper

```

AutoMapping
-----------

[](#automapping)

For this example, we want to be able to easily map between `Employee` and `EmployeeDto` models.

### Example models

[](#example-models)

```
namespace App\Models;

class Employee
{
    protected $firstName;
    protected $lastName;
    protected $birthYear;

    public function __construct($firstName = null, $lastName = null, $birthYear = null)
    {
        $this->firstName = $firstName;
        $this->lastName = $lastName;
        $this->birthYear = $birthYear;
    }

    public function getFirstName() { return $this->firstName; }
    public function getLastName() { return $this->lastName; }
    public function getBirthYear() { return $this->birthYear; }
}

class EmployeeDto
{
    protected $firstName;
    protected $lastName;
    protected $birthYear;
}
```

### Registering the mappings

[](#registering-the-mappings)

We can register the mappings in our `boot` method in any `ServiceProvider`

```
namespace App\Providers;

use App\Models\Employee;
use App\Models\EmployeeDto;
use Illuminate\Support\ServiceProvider;
use Skraeda\AutoMapper\Support\Facades\AutoMapperFacade as AutoMapper;;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register AutoMapper mappings.
     *
     * @see https://github.com/mark-gerarts/automapper-plus#registering-mappings
     * @return void
     */
    public function boot()
    {
        AutoMapper::getConfiguration()
                  ->registerMapping(Employee::class, EmployeeDto::class)
                  ->reverseMap();
    }
}
```

### Mapping between the models

[](#mapping-between-the-models)

Using the facade or alias

```
use Skraeda\AutoMapper\Support\Facades\AutoMapperFacade as AutoMapper

$dto = AutoMapper::map(new Employee("John", "Doe", 1980), EmployeeDto::class);
$john = AutoMapper::map($dto, Employee::class);
```

Using the contract

```
use Skraeda\AutoMapper\Contracts\AutoMapperContract;

$mapper = App::make(AutoMapperContract::class);
$dto = $mapper->map(new Employee("John", "Doe", 1980), EmployeeDto::class);
$john = $mapper->map($dto, Employee::class);
```

Using the helper functions

```
$dto = auto_map(new Employee("John", "Doe", 1980), EmployeeDto::class);
$john = auto_map($dto, Employee::class);
```

Custom Mappers
--------------

[](#custom-mappers)

`AutoMapper+` allows us to define separate classes to perform the mapping if it requires more complicated logic or if we need better performance.

Let's change `EmployeeDto` to look like this instead

```
class EmployeeDto
{
    protected $fullName;
    protected $age;

    public function setFullName($fullName) { $this->fullName = $fullName; }
    public function setAge($age) { $this->age = $age; }
}
```

To teach the `AutoMapper+` how to use a custom mapper to map to the new `EmployeeDto` model then delete the old mapping and create a new custom mapper.

```
namespace App\Mappings;

use AutoMapperPlus\CustomMapper\CustomMapper;
use Illuminate\Support\Carbon;

class EmployeeToDtoMapper extends CustomMapper
{
    /**
     * Map properties of source to destination.
     *
     * @param array|object $source
     * @param object $destination
     * @param array $context
     * @return mixed
     */
    public function mapToObject($source, $destination, array $context = [])
    {
        $destination->setFullName(sprintf("%s %s", $source->getFirstName(), $source->getLastName()));
        $destination->setAge(Carbon::now()->subYears($source->getBirthYear())->year);
        return $destination;
    }
}
```

You can manually register this custom mapping in your `ServiceProvider` or you can use a `mapping.php` config file.

Publish the config file

```
php artisan vendor:publish --provider=Skraeda\\AutoMapper\\Providers\\AutoMapperServiceProvider

```

Register custom mappers

```
'custom' => [
    \App\Mappings\EmployeeToDtoMapper::class => [
        'source' => \App\Models\Employee::class,
        'target' => \App\Models\EmployeeDto::class
    ]
]
```

Starting with Version 2, you can have Custom Mappers automatically discovered by enabling directory scanning in the `mapping.php` config file. Mappers found within the directories you specify in `mapping.php` with the `Maps` attribute will be automatically registered.

```
namespace App\Mappings;

use App\Models\Employee;
use App\Models\EmployeeDto;
use Skraeda\AutoMapper\Attributes\Maps;

#[Maps(Employee::class, EmployeeDto::class)]
class EmployeeToDtoMapper extends CustomMapper
{
    // Code..
}
```

If you have multiple mappers discovered this way, you may want to turn on caching for your production environment within the `mapping.php` config file. With caching enabled, the first request will scan your files for mappers and store them in a cache file (default: app\_dir/storage/app/framework/automapper/automapper.php) that's loaded for the next requests.

You can use `php artisan mapping:clear` to clear the mapping cache directory if you add new mappers.

You can also use `php artisan mapping:cache` to immediately scan and cache the mappers.

Helpers and methods
-------------------

[](#helpers-and-methods)

### Collection macro

[](#collection-macro)

You can use the `autoMap` method on a collection to map into some target class.

```
use Illuminate\Support\Collection;

$employees = Collection::make([new Employee("John", "Doe", 1980), new Employee("Jane", "Doe", 1985)]);
$dtos = $employees->autoMap(EmployeeDto::class);
```

### Generator command

[](#generator-command)

You can use the `make:mapper` artisan command to generate the boilercode for a custom mapper.

```
php artisan make:mapper EmployeeToDtoMapper

```

### AutoMapperContract

[](#automappercontract)

```
public function map($source, $targetClass, array $context = []);
public function mapToObject($source, $target, array $context = []);
public function mapMultiple($collection, string $targetClass, array $context = []): \Illuminate\Support\Collection;
public function getConfiguration(): \AutoMapperPlus\Configuration\AutoMapperConfigInterface;
public function registerCustomMapper(string $mapper, string $source, string $target): void;
```

### AutoMapperFacade

[](#automapperfacade)

```
/**
 * @method static mixed map($source, $targetClass, array $context)
 * @method static mixed mapToObject($source, $target, array $context)
 * @method static \Illuminate\Support\Collection mapMultiple($collection, string $targetClass, array $context)
 * @method static \AutoMapperPlus\Configuration\AutoMapperConfigInterface getConfiguration()
 * @method static void registerCustomMapper(string $mapper, string $source, string $target)
 */
```

### Helper functions

[](#helper-functions)

```
function auto_map($source, $targetClass, array $context = []);
function auto_map_to_object($source, $target, array $context = []);
function auto_map_multiple($collection, string $targetClass, array $context = []): \Illuminate\Support\Collection;
```

Testing
-------

[](#testing)

### Run

[](#run)

```
vendor/bin/phpunit

```

Alternatively

```
composer test

```

### Generate coverage

[](#generate-coverage)

```
vendor/bin/phpunit --coverage-html=coverage --log-junit coverage/report.xml

```

Alternatively

```
composer test:coverage

```

### Run static analysis

[](#run-static-analysis)

```
vendor/bin/phpstan analyse -l 1 src tests

```

Alternatively

```
composer stan

```

Changelog
---------

[](#changelog)

### V2.0.0

[](#v200)

- Add the `Maps` attribute
- Adds several interfaces to scan and cache custom mappers discovered with the `Maps` attribute
- Adds config options to enable scanning and caching
- Adds artisan commands `mapping:clear` and `mapping:cache` to manage mapping cache
- Adds method `registerCustomMapper` to \\Skraeda\\AutoMapper\\Contracts\\AutoMapperContract

### v1.2.0

[](#v120)

- Add Laravel 8 support

### V1.1.0

[](#v110)

###  Health Score

52

—

FairBetter than 96% of packages

Maintenance72

Regular maintenance activity

Popularity31

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity76

Established project with proven stability

 Bus Factor1

Top contributor holds 80% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~264 days

Recently: every ~230 days

Total

9

Last Release

155d ago

Major Versions

v1.2.2 → v3.0.02025-12-14

PHP version history (2 changes)v1.0.0PHP &gt;=7.1.3

v3.0.0PHP ^8.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/15886771?v=4)[Gunnar Örn Baldursson](/maintainers/Gunsobal)[@Gunsobal](https://github.com/Gunsobal)

---

Top Contributors

[![Gunsobal](https://avatars.githubusercontent.com/u/15886771?v=4)](https://github.com/Gunsobal "Gunsobal (36 commits)")[![eythork](https://avatars.githubusercontent.com/u/15385409?v=4)](https://github.com/eythork "eythork (3 commits)")[![eythork-sjukraskra](https://avatars.githubusercontent.com/u/66421417?v=4)](https://github.com/eythork-sjukraskra "eythork-sjukraskra (3 commits)")[![stefanarnarss](https://avatars.githubusercontent.com/u/128060556?v=4)](https://github.com/stefanarnarss "stefanarnarss (2 commits)")[![zozidalom](https://avatars.githubusercontent.com/u/24374345?v=4)](https://github.com/zozidalom "zozidalom (1 commits)")

---

Tags

laravelconverterautomapperobject-to-object

###  Code Quality

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/skraeda-laravel-automapper/health.svg)

```
[![Health](https://phpackages.com/badges/skraeda-laravel-automapper/health.svg)](https://phpackages.com/packages/skraeda-laravel-automapper)
```

###  Alternatives

[barryvdh/laravel-ide-helper

Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.

14.9k123.0M687](/packages/barryvdh-laravel-ide-helper)[wnx/laravel-stats

Get insights about your Laravel Project

1.8k1.8M7](/packages/wnx-laravel-stats)[tehwave/laravel-achievements

Simple, elegant Achievements the Laravel way

7012.8k](/packages/tehwave-laravel-achievements)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

245.2k](/packages/aedart-athenaeum)[interaction-design-foundation/laravel-geoip

Support for multiple Geographical Location services.

17221.0k3](/packages/interaction-design-foundation-laravel-geoip)[tomshaw/electricgrid

A feature-rich Livewire package designed for projects that require dynamic, interactive data tables.

116.6k](/packages/tomshaw-electricgrid)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
