{"id":19324811,"url":"https://github.com/spatie/laravel-morph-map-generator","last_synced_at":"2025-05-16T16:08:39.712Z","repository":{"id":37908496,"uuid":"308281869","full_name":"spatie/laravel-morph-map-generator","owner":"spatie","description":"Automatically generate morph maps in your Laravel application","archived":false,"fork":false,"pushed_at":"2025-02-06T11:53:38.000Z","size":72,"stargazers_count":67,"open_issues_count":0,"forks_count":11,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-04-12T15:56:54.129Z","etag":null,"topics":["laravel","morph-maps","php","spatie"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/spatie.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"spatie"}},"created_at":"2020-10-29T09:42:20.000Z","updated_at":"2025-04-04T04:40:57.000Z","dependencies_parsed_at":"2024-02-29T09:26:42.593Z","dependency_job_id":"68f5fca4-a18b-4a34-81d7-32229da1c4eb","html_url":"https://github.com/spatie/laravel-morph-map-generator","commit_stats":{"total_commits":56,"total_committers":13,"mean_commits":"4.3076923076923075","dds":0.5178571428571428,"last_synced_commit":"8f87d911efd9723863ff246d280c6132af601376"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-morph-map-generator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-morph-map-generator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-morph-map-generator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-morph-map-generator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spatie","download_url":"https://codeload.github.com/spatie/laravel-morph-map-generator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253914170,"owners_count":21983442,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["laravel","morph-maps","php","spatie"],"created_at":"2024-11-10T02:06:51.686Z","updated_at":"2025-05-16T16:08:39.693Z","avatar_url":"https://github.com/spatie.png","language":"PHP","funding_links":["https://github.com/sponsors/spatie"],"categories":[],"sub_categories":[],"readme":"# Automatically generate morph maps in your Laravel application\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/laravel-morph-map-generator.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-morph-map-generator)\n![Tests](https://github.com/spatie/laravel-morph-map-generator/workflows/Tests/badge.svg)\n![Psalm](https://github.com/spatie/laravel-morph-map-generator/workflows/Psalm/badge.svg)\n![Check \u0026 fix styling](https://github.com/spatie/laravel-morph-map-generator/workflows/Check%20\u0026%20fix%20styling/badge.svg)\n[![Total Downloads](https://img.shields.io/packagist/dt/spatie/laravel-morph-map-generator.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-morph-map-generator)\n\nWith this package, you shouldn't worry about forgetting to add models to your application's morph map. Each model will autoregister itself in the morph map. The only thing you should do is implementing the `getMorphClass` method on your models like this:\n\n```php\nclass Post extends Model\n{\n    public function getMorphClass(): string {\n        return 'post';\n    }\n}\n```\n\nFrom now on, the `Post` model will be represented as `post` within your morph map.\n\n## Support us\n\n[\u003cimg src=\"https://github-ads.s3.eu-central-1.amazonaws.com/laravel-morph-map-generator.jpg?t=1\" width=\"419px\" /\u003e](https://spatie.be/github-ad-click/laravel-morph-map-generator)\n\nWe invest many resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).\n\nWe highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require spatie/laravel-morph-map-generator\n```\n\nYou can publish the config file with:\n\n```bash\nphp artisan vendor:publish --provider=\"Spatie\\LaravelMorphMapGenerator\\MorphMapGeneratorServiceProvider\" --tag=\"config\"\n```\n\nThis is the contents of the published config file:\n\n```php\nreturn [\n    /*\n    * When enabled, morph maps will be automatically generated when the\n    * application is booted.\n    */\n\n    'autogenerate' =\u003e true,\n\n    /**\n     * Change the base directory if the models don't reside in the default App namespace.\n     *\n     * For example, the base directory would become 'src' if:\n     * - Application is in src/App\n     * - Models are in src/Domain\n     */\n\n    'base_directory' =\u003e '/',\n\n    /*\n    * Within these paths, the package will search for models to be included\n    * in the generated morph map.\n    */\n\n    'paths' =\u003e [\n        app_path(),\n    ],\n\n    /*\n    * Only models that extend from one of the base models defined here will\n    * be included in the generated morph map.\n    */\n\n    'base_models' =\u003e [\n        Illuminate\\Database\\Eloquent\\Model::class,\n    ],\n\n    /*\n    * When generating the morph map, these models will not be included.\n    */\n\n    'ignored_models' =\u003e [],\n\n\n    /*\n    * Morph maps can be cached, there's a `FilesystemMorphMapCacheDriver` which\n    * stores the morph map as a file in a directory or you can also use the\n    * Laravel built-in cache by using `LaravelMorphMapCacheDriver`.\n    *\n    * Both drivers have their own config:\n    * - `FilesystemMorphMapCacheDriver` requires a `path` to store the file\n    * - `LaravelMorphMapCacheDriver` requires a `key` for storage\n    */\n\n    'cache' =\u003e [\n        'type' =\u003e Spatie\\LaravelMorphMapGenerator\\Cache\\FilesystemMorphMapCacheDriver::class,\n        'path' =\u003e storage_path('app/morph-map-generator'),\n    ],\n];\n```\n\n## Usage\n\nFirst, you have to implement `getMorphClass` for the models you want to include in your morph map. We suggest you create a new base model class in your application from which all your models extend. So you could throw an exception when `getMorphClass` was not yet implemented:\n\n```php\nuse Illuminate\\Database\\Eloquent\\Model;\n\nabstract class BaseModel extends Model\n{\n    public function getMorphClass()\n    {\n        throw new Exception('The model should implement `getMorphClass`');\n    }\n}\n```\n\nWhen a model is not implementing `getMorphClass`, it will throw an exception when building the generated morph map, making it possible to quickly find models that do not have a morph map entry.\n\nWhen `autogenerate` is enabled in the `morph-map-generator` config file, the morph map in your application will be dynamically generated each time the application boots. This is great in development environments since each time your application boots, the morph map is regenerated. For performance reasons, you should cache the dynamically generated morph map by running the following command:\n\n```bash\nphp artisan morph-map:cache\n```\n\nRemoving a cached morph map can be done by running:\n\n```php\nphp artisan morph-map:clear\n```\n\nWhen using Laravel 11+, the morph map can also be cached by using Laravel's optimize commands: `optimize` and `optimize:clear`\n\n### Using a custom resolver\n\nYou can also determine morph class values programmatically by using a custom resolver. For example, you could use the following to automatically derive the value based on the singular form of the model's table name:\n\n```php\nMorphMapGenerator::resolveUsing(fn ($model) =\u003e Str::singular($model-\u003egetTable()));\n```\n\nBe warned! When the output of the closure above is not stable then you'll manually need to update all the `morhp_type` columns within your database. Using something like the table name is a good idea since those do not change that often.\n\nYou may set the resolver in the `boot` method of one of your service providers.\n\n### Models outside your path\n\nSome models like the default Laravel User model and models defined by packages will not be discovered by this package since it only searches for models within the app path and not the complete vendor directory. You can include these models in your morph map by using the default [morph map](https://laravel.com/docs/9.x/eloquent-relationships#custom-polymorphic-types) feature from Laravel:\n\n```php\nRelation::enforceMorphMap([\n    'post' =\u003e 'App\\Models\\Post',\n    'video' =\u003e 'App\\Models\\Video',\n]);\n```\n\n## Testing\n\n``` bash\ncomposer test\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Credits\n\n- [Ruben Van Assche](https://github.com/rubenvanassche)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Flaravel-morph-map-generator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspatie%2Flaravel-morph-map-generator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Flaravel-morph-map-generator/lists"}