gigerit/laravel-morphmap-js-generator
Larablocks Analysis
gigerit/laravel-morphmap-js-generator works with Laravel 8 to 13 (latest release v1.2.2, Oct 4, 2026).
Recommendation checks
PromisingPromising: maintained and up to date, meets 2 of 4 checks; not yet widely used.
- Works with Laravel 13, the current version
- Released in the last 12 months (Oct 4, 2026)
- 256 downloads a month (needs 10,000)
- 0 GitHub stars (needs 100)
State
Actively maintained- Last stable release: Oct 4, 2026.
- 3 releases in the last 12 months.
- First released: Oct 30, 2024.
- Monthly downloads: 256.
- 1 open issue on GitHub.
Data from Packagist and GitHub, checked by Larablocks on Nov 30, 2026.
| Install | |
|---|---|
composer require gigerit/laravel-morphmap-js-generator |
|
| Latest Version: | v1.2.2 |
| PHP: | ^8.0 |
| Laravel: | 8 – 13 |
| License: | MIT |
| Last Updated: | Nov 15, 2026 |
| Links: | GitHub · Packagist |
Laravel Morph Map JS Generator
Generate frontend JavaScript/TypeScript constants from your Laravel application's morph map configuration. Keep your frontend morph types in sync with your Laravel models automatically.
Installation
You can install the package via composer:
composer require gigerit/laravel-morphmap-js-generator --dev
The package will automatically register itself with Laravel.
Usage
After installation, you can generate the JavaScript/TypeScript constants using the artisan command:
# Generate JavaScript file
php artisan morphmap:generate-js
# Generate TypeScript file
php artisan morphmap:generate-js --ts
# Specify custom output path
php artisan morphmap:generate-js --path=resources/js/constants
Example
If your Laravel application has this morph map configuration:
// App/Providers/AppServiceProvider.php
use Illuminate\Database\Eloquent\Relations\Relation;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
Relation::morphMap([
'tenant' => Tenant::class,
'user' => User::class,
1 => Client::class,
2 => Contact::class,
]);
}
}
With --ts, the command generates these TypeScript constants:
// morphMap.ts
export const MORPH_MAP = {
TENANT: 'tenant',
USER: 'user',
CLIENT: 1,
CONTACT: 2
} as const;
export const MORPH_MAP_MODELS = {
[MORPH_MAP.TENANT]: https://github.com/gigerIT/laravel-morphmap-js-generator/blob/HEAD/'Tenant',
[MORPH_MAP.USER]: https://github.com/gigerIT/laravel-morphmap-js-generator/blob/HEAD/'User',
[MORPH_MAP.CLIENT]: https://github.com/gigerIT/laravel-morphmap-js-generator/blob/HEAD/'Client',
[MORPH_MAP.CONTACT]: https://github.com/gigerIT/laravel-morphmap-js-generator/blob/HEAD/'Contact'
} as const;
export const getMorphMapModel = (morphMap: MorphMapValue): string => {
return MORPH_MAP_MODELS[morphMap] || 'Unknown';
};
export type MorphMapValue = typeof MORPH_MAP[keyof typeof MORPH_MAP];
Without --ts, the command writes morphMap.js with the same runtime exports
(MORPH_MAP, MORPH_MAP_MODELS, and getMorphMapModel) as executable JavaScript,
without TypeScript annotations or the MorphMapValue type export.
The helper accepts morph values such as 'user' or 1. Constant names such as
'USER' are only valid arguments if they are also morph values. Untyped callers
passing an unknown value retain the 'Unknown' runtime fallback.
Using in Your Frontend
import { MORPH_MAP, getMorphMapModel, type MorphMapValue } from './morphMap';
// Use constants
if (type === MORPH_MAP.TENANT) {
// Handle tenant case
}
// Get model name
const modelName = getMorphMapModel(MORPH_MAP.CLIENT); // Returns 'Client'
// TypeScript support
function handleMorphable(type: MorphMapValue) {
// Type-safe handling of morph types
}
Features
- 🔄 Automatically syncs with Laravel's morph map configuration
- 📝 Generates JavaScript or TypeScript files
- 🎯 Type-safe with TypeScript support
- 🔍 Includes helper function for model name lookup
- 🎨 Converts numeric keys to readable constant names
- 📦 Zero configuration required
- 💻 Command output shows current mappings
Command Options
| Option | Description | Default |
|---|---|---|
--ts |
Generate TypeScript instead of JavaScript | false |
--path |
Custom output path for generated file | resources/js |
Best Practices
- Version Control: Add the generated file to your version control system to track changes.
- Build Process: Include the generation command in your build process to ensure sync:
{
"scripts": {
"prepare": "php artisan morphmap:generate-js --ts"
}
}
- Type Safety: Use TypeScript for better type checking and IDE support.
Development
Install PHP dependencies with composer install and the test compiler with
npm ci, then run npm test. The tests boot the package service provider,
execute morphmap:generate-js with and without --ts, parse and execute the
generated JavaScript without Node type stripping, and compile and execute the
generated TypeScript under strict settings. Both paths verify lookups and the
runtime fallback. See the test workflow for the
tested PHP and Laravel versions.
Security
If you discover any security related issues, please email security@example.com instead of using the issue tracker.
License
The MIT License (MIT). Please see License File for more information.
Alternatives to gigerit/laravel-morphmap-js-generator
A Laravel package for generating TypeScript/JavaScript enums and frontend transl...
A Laravel package to export PHP Enums to TypeScript with labels, metadata, and t...
Version History
| Version | Released | PHP | Laravel | License |
|---|---|---|---|---|
| v1.2.2 | ^8.0 | ^8.0| | MIT | |
| v1.2.1 | ^8.0 | ^8.0| | MIT | |
| v1.2.0 | ^8.0 | ^8.0| | MIT | |
| v1.1.0 | ^8.0 | ^8.0| | MIT | |
| v1.0.1 | ^8.0 | ^8.0| | MIT | |
| v1.0.0 | ^8.0 | ^8.0| | MIT |