PHPackages                             namchung/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. namchung/laravel-auto-morph-map

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

namchung/laravel-auto-morph-map
===============================

Automatically alias and map the polymorphic types of Eloquent models.

5.0.0(6y ago)01MITPHPPHP ^7.3

Since Aug 7Pushed 5y agoCompare

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

READMEChangelogDependencies (5)Versions (13)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://camo.githubusercontent.com/1d0d665a40b59290e0029c99300b6c2065e2cbee4a49ad8b4f87d9bcb00ac18f/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7365626173746961616e6c7563612f6c61726176656c2d6175746f2d6d6f7270682d6d61702f6d61737465722e737667)](https://travis-ci.org/sebastiaanluca/laravel-auto-morph-map)[![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://blog.sebastiaanluca.com)[![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 7.3 or higher
- Laravel 6.0 or higher

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

[](#how-to-install)

Via Composer:

```
composer require sebastiaanluca/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 `\SebastiaanLuca\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 `\SebastiaanLuca\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://www.sebastiaanluca.com) for more information, [my blog](https://blog.sebastiaanluca.com) 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

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity1

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~76 days

Recently: every ~109 days

Total

11

Last Release

2067d ago

Major Versions

1.0.0 → 2.0.02018-08-10

2.1.0 → 3.0.02019-02-28

3.0.3 → 4.0.02019-09-06

4.0.1 → 5.0.02020-04-24

PHP version history (2 changes)1.0.0PHP ^7.2

5.0.0PHP ^7.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/93f87d76a68ff98547478e274d69bea66c87cf337c18784c42f8b00cc7a69b71?d=identicon)[namchung](/maintainers/namchung)

---

Top Contributors

[![sebastiaanluca](https://avatars.githubusercontent.com/u/711940?v=4)](https://github.com/sebastiaanluca "sebastiaanluca (50 commits)")

---

Tags

phplaravelmodeleloquentclassmapmorph

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[spiritix/lada-cache

A Redis based, automated and scalable database caching layer for Laravel

591444.8k2](/packages/spiritix-lada-cache)[sebastiaanluca/laravel-boolean-dates

Automatically convert Eloquent model boolean attributes to dates (and back).

40111.7k1](/packages/sebastiaanluca-laravel-boolean-dates)[matchory/elasticsearch

The missing elasticsearch ORM for Laravel!

3059.0k](/packages/matchory-elasticsearch)[kiwilan/typescriptable-laravel

PHP package for Laravel to type Eloquent models, routes, Spatie Settings with autogenerated TypeScript. If you want to use some helpers with Inertia, you can install associated NPM package.

3920.9k](/packages/kiwilan-typescriptable-laravel)[waad/laravel-model-metadata

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

823.1k](/packages/waad-laravel-model-metadata)

PHPackages © 2026

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