PHPackages                             leandrodiogenes/laravel-auto-morph-map - 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. [Database &amp; ORM](/categories/database)
4. /
5. leandrodiogenes/laravel-auto-morph-map

ActiveLibrary[Database &amp; ORM](/categories/database)

leandrodiogenes/laravel-auto-morph-map
======================================

Automatically alias and map the polymorphic types of Eloquent models.

9.0.0(3y ago)026MITPHPPHP ^8.0

Since Aug 7Pushed 3y agoCompare

[ Source](https://github.com/leandrodiogenes/laravel-auto-morph-map)[ Packagist](https://packagist.org/packages/leandrodiogenes/laravel-auto-morph-map)[ Docs](https://github.com/sebastiaanluca/laravel-auto-morph-map)[ RSS](/packages/leandrodiogenes-laravel-auto-morph-map/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (3)Dependencies (5)Versions (20)Used By (0)

Automatically alias and map the polymorphic types of Eloquent models
====================================================================

[](#automatically-alias-and-map-the-polymorphic-types-of-eloquent-models)

[![Latest stable release](https://camo.githubusercontent.com/01afc94f26c8dda604cd1840f363c7f523b2742912663ba072005d04f82b6a61/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7365626173746961616e6c7563612f6c61726176656c2d6175746f2d6d6f7270682d6d61702e7376673f6c6162656c3d737461626c65)](https://packagist.org/packages/sebastiaanluca/laravel-auto-morph-map)[![Software license](https://camo.githubusercontent.com/08662bb339bee903c7279b82f3217310d3f196fdfa63b019d294ae85e1a5d7d7/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d696e666f726d6174696f6e616c2e737667)](LICENSE.md)[![Build status](https://github.com/sebastiaanluca/laravel-auto-morph-map/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/sebastiaanluca/laravel-auto-morph-map/actions/workflows/test.yml?query=branch%3Amaster)[![Total downloads](https://camo.githubusercontent.com/d5b35739e9980e568f54026c5cd84f5bc36e43edada536d126550051ba9bcf84/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7365626173746961616e6c7563612f6c61726176656c2d6175746f2d6d6f7270682d6d61702e7376673f636f6c6f723d627269676874677265656e)](https://packagist.org/packages/sebastiaanluca/laravel-auto-morph-map)[![Total stars](https://camo.githubusercontent.com/d9e939fc8a9ce9fdd5e02e2603152916b4ef1722818638f5a2b0208776439f50/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f7365626173746961616e6c7563612f6c61726176656c2d6175746f2d6d6f7270682d6d61702e7376673f636f6c6f723d627269676874677265656e)](https://github.com/sebastiaanluca/laravel-auto-morph-map)

[![Read my blog](https://camo.githubusercontent.com/0760b582e22202bd412feb64f5f87b6459f64a0b8ea5fdd24cbadf87df736177/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c696e6b2d626c6f672d6c69676874677265792e737667)](https://sebastiaanluca.com/blog)[![View my other packages and projects](https://camo.githubusercontent.com/5ce3a83ee0ec2b4ae47a99f67347c1d8e78582757eca80fe31c98e5bd0e2185a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c696e6b2d6f746865725f7061636b616765732d6c69676874677265792e737667)](https://packagist.org/packages/sebastiaanluca)[![Follow @sebastiaanluca on Twitter](https://camo.githubusercontent.com/bde346ed8efc8b2458386b36256c64e3fe010276f449b80fe4f663c4c8bbd83e/68747470733a2f2f696d672e736869656c64732e696f2f747769747465722f666f6c6c6f772f7365626173746961616e6c7563612e7376673f7374796c653d736f6369616c)](https://twitter.com/sebastiaanluca)[![Share this package on Twitter](https://camo.githubusercontent.com/cb820a0ecc9645168e33b03925d7f14691262ddbaeaf66a0a91697803d0cba2d/68747470733a2f2f696d672e736869656c64732e696f2f747769747465722f75726c2f687474702f736869656c64732e696f2e7376673f7374796c653d736f6369616c)](https://twitter.com/intent/tweet?text=Automatically%20alias%20and%20map%20the%20polymorphic%20types%20of%20Eloquent%20models.%20Via%20@sebastiaanluca%20https://github.com/sebastiaanluca/laravel-auto-morph-map)

**Decouple your internal application namespace structure from your database by automatically aliasing and mapping your Eloquent models as short, uniform class names instead of full class namespaces.**

> By default, Laravel will use the fully qualified class name to store the type of the related model. For instance, given the example above where a `Comment` may belong to a `Post` or a `Video`, the default `commentable_type` would be either `App\Post` or `App\Video`, respectively. However, you may wish to decouple your database from your application's internal structure. In that case, you may define a relationship "morph map" to instruct Eloquent to use a custom name for each model instead of the class name.

See [Custom Polymorphic Types](https://laravel.com/docs/5.6/eloquent-relationships#polymorphic-relations) in the Laravel documentation for more information.

*Laravel auto morph map* improves upon that by scanning all your Eloquent models, automatically aliasing them as *uniform singular table names*, and registering them as a polymorphic type. No more need for dozens of manual `Relation::morphMap()` calls to register model morph types and no need to maintain that pesky morph map list when you add, change, or remove a model.

Table of contents
-----------------

[](#table-of-contents)

- [Requirements](#requirements)
- [How to install](#how-to-install)
- [How to use](#how-to-use)
    - [Defining model namespaces](#defining-model-namespaces)
    - [Overriding existing aliases](#overriding-existing-aliases)
    - [Caching morph types in production](#caching-morph-types-in-production)
    - [Configuration](#configuration)
        - [Naming](#naming)
        - [Casing](#casing)
- [License](#license)
- [Change log](#change-log)
- [Testing](#testing)
- [Contributing](#contributing)
- [Security](#security)
- [Credits](#credits)
- [About](#about)

Requirements
------------

[](#requirements)

- PHP 8 or higher
- Laravel 8 or higher

How to install
--------------

[](#how-to-install)

Via Composer:

```
composer require leandrodiogenes/laravel-auto-morph-map
```

How to use
----------

[](#how-to-use)

After installing this package, you're immediately good to go! The package will scan all your models and automatically register their polymorphic types on-the-fly.

Besides scanning and aliasing your models for you, this package alters no native Laravel functionality. Therefore, see the Laravel documentation on how to use [custom polymorphic types](https://laravel.com/docs/5.6/eloquent-relationships#polymorphic-relations).

If you wish to customize some behavior, read further.

### Defining model namespaces

[](#defining-model-namespaces)

*Laravel auto morph map* uses your `composer.json` PSR-4 autoload section to know which namespaces and paths to scan. In any new Laravel project, the default `App\\` namespace is already in place, so for most projects no additional setup required. If you have other namespaces registered like local modules or (dev) packages, those will be scanned too.

```
{
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "MyModule\\": "modules/MyModule/",
            "MyPackage\\": "MyPackage/src/"
        }
    }
}
```

Furthermore it filters out traits, abstract classes, helper files, and other unusable items to only bind valid Eloquent models.

### Overriding existing aliases

[](#overriding-existing-aliases)

If you wish, you can still override already aliased morph types in your service provider's `boot` method like you would normally:

```
use Illuminate\Database\Eloquent\Relations\Relation;

Relation::morphMap([
    'posts' => \App\Post::class,
    'videos' => \App\Video::class,
]);
```

These will always have priority over the already defined ones.

### Caching morph types in production

[](#caching-morph-types-in-production)

To cache all aliases and speed up your application in production, add the cache command to your deploy scripts:

```
php artisan morphmap:cache

```

This scans all your current models and writes a static cache file to the `bootstrap/cache` directory. Upon subsequent framework booting, it reads the cache file instead of scanning and aliasing on-the-fly.

Note that this thus **disables runtime scanning**, meaning new models will not be recognized and changes to existing models will not be reflected (not very handy during development).

To clear the cache file, run:

```
php artisan morphmap:clear

```

### Configuration

[](#configuration)

Run

```
php artisan vendor:publish

```

and select

```
laravel-auto-morph-map (configuration)

```

to publish the configuration file.

#### Naming

[](#naming)

The naming scheme to use when determining the model's morph type base value. Defaults to the singular table name (automatically determined by Laravel or overridden in the model using the `$table` variable).

You can change this to use the singular table name, table name, or class basename. See `\LeandroDiogenes\AutoMorphMap\Constants\NamingSchemes` for possible options.

Singular table name (default):

```
Relation::morphMap([
    'collection_item' => 'App\CollectionItem',
]);
```

Table name:

```
Relation::morphMap([
    'collection_items' => 'App\CollectionItem',
]);
```

Class basename:

```
Relation::morphMap([
    'CollectionItem' => 'App\CollectionItem',
]);
```

#### Casing

[](#casing)

Converts your model name (after having passed the naming scheme conversion) to a more uniform string. By default, the model's name (based on your naming scheme) is converted to *snake case*.

You can change this to use snake, slug, camel, studly or no casing. See `\LeandroDiogenes\AutoMorphMap\Constants\CaseTypes` for possible options.

Snake case (default):

```
Relation::morphMap([
    'collection_item' => 'App\CollectionItem',
]);
```

Slug case:

```
Relation::morphMap([
    'collection-item' => 'App\CollectionItem',
]);
```

Camel case:

```
Relation::morphMap([
    'collectionItem' => 'App\CollectionItem',
]);
```

Studly case:

```
Relation::morphMap([
    'CollectionItem' => 'App\CollectionItem',
]);
```

None (determined by your naming scheme and Laravel's class-to-table conversion method):

```
Relation::morphMap([
    'collection_item' => 'App\CollectionItem',
]);
```

License
-------

[](#license)

This package operates under the MIT License (MIT). Please see [LICENSE](LICENSE.md) for more information.

Change log
----------

[](#change-log)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Testing
-------

[](#testing)

```
composer install
composer test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) and [CODE OF CONDUCT](CODE_OF_CONDUCT.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Sebastiaan Luca](https://github.com/sebastiaanluca)
- [All Contributors](../../contributors)

About
-----

[](#about)

My name is Sebastiaan and I'm a freelance back-end developer specializing in building custom Laravel applications. Check out my [portfolio](https://sebastiaanluca.com) for more information, [my blog](https://sebastiaanluca.com/blog) for the latest tips and tricks, and my other [packages](https://packagist.org/packages/sebastiaanluca) to kick-start your next project.

Have a project that could use some guidance? Send me an e-mail at !

###  Health Score

31

—

LowBetter than 66% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity76

Established project with proven stability

 Bus Factor1

Top contributor holds 91.1% 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 ~91 days

Recently: every ~101 days

Total

17

Last Release

1431d ago

Major Versions

3.0.3 → 4.0.02019-09-06

4.0.1 → 5.0.02020-04-24

5.0.0 → 6.0.02020-10-19

6.0.0 → 7.0.02021-06-22

v6.x-dev → 9.0.02022-08-01

PHP version history (3 changes)1.0.0PHP ^7.2

5.0.0PHP ^7.3

7.0.0PHP ^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/2463299?v=4)[Leandro Diógenes](/maintainers/leandrodiogenes)[@leandrodiogenes](https://github.com/leandrodiogenes)

---

Top Contributors

[![sebastiaanluca](https://avatars.githubusercontent.com/u/711940?v=4)](https://github.com/sebastiaanluca "sebastiaanluca (72 commits)")[![leandrodiogenes](https://avatars.githubusercontent.com/u/2463299?v=4)](https://github.com/leandrodiogenes "leandrodiogenes (4 commits)")[![emielmolenaar](https://avatars.githubusercontent.com/u/2470795?v=4)](https://github.com/emielmolenaar "emielmolenaar (2 commits)")[![reziamini](https://avatars.githubusercontent.com/u/29504334?v=4)](https://github.com/reziamini "reziamini (1 commits)")

---

Tags

phplaravelmodeleloquentclassmapmorph

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/leandrodiogenes-laravel-auto-morph-map/health.svg)

```
[![Health](https://phpackages.com/badges/leandrodiogenes-laravel-auto-morph-map/health.svg)](https://phpackages.com/packages/leandrodiogenes-laravel-auto-morph-map)
```

###  Alternatives

[anourvalar/eloquent-serialize

Laravel Query Builder (Eloquent) serialization

11223.5M33](/packages/anourvalar-eloquent-serialize)[waad/laravel-model-metadata

A robust Laravel package for handling metadata with JSON casting, custom relation names, and advanced querying capabilities.

854.6k](/packages/waad-laravel-model-metadata)[matchory/elasticsearch

The missing elasticsearch ORM for Laravel!

3064.0k](/packages/matchory-elasticsearch)[wayofdev/laravel-cycle-orm-adapter

🔥 A Laravel adapter for CycleORM, providing seamless integration of the Cycle DataMapper ORM for advanced database handling and object mapping in PHP applications.

3535.8k3](/packages/wayofdev-laravel-cycle-orm-adapter)[salehhashemi/laravel-repository

Implementing the repository pattern for Laravel projects.

2010.5k](/packages/salehhashemi-laravel-repository)

PHPackages © 2026

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