PHPackages                             gigerit/laravel-morphmap-js-generator - 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. gigerit/laravel-morphmap-js-generator

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

gigerit/laravel-morphmap-js-generator
=====================================

Generate frontend JavaScript/TypeScript constants from Laravel morph map configuration

v1.2.0(1mo ago)0482MITPHPPHP ^8.0CI passing

Since Oct 30Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/gigerIT/laravel-morphmap-js-generator)[ Packagist](https://packagist.org/packages/gigerit/laravel-morphmap-js-generator)[ RSS](/packages/gigerit-laravel-morphmap-js-generator/feed)WikiDiscussions main Synced today

READMEChangelog (4)Dependencies (2)Versions (6)Used By (0)

Laravel Morph Map JS Generator
==============================

[](#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
------------

[](#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
-----

[](#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

[](#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,
        ]);
    }
}
```

The command will generate a JavaScript/TypeScript file with these constants:

```
// morphMap.ts or morphMap.js

export const MORPH_MAP = {
  TENANT: 'tenant',
  USER: 'user',
  CLIENT: 1,
  CONTACT: 2
} as const;

export const MORPH_MAP_MODELS = {
  [MORPH_MAP.TENANT]: 'Tenant',
  [MORPH_MAP.USER]: 'User',
  [MORPH_MAP.CLIENT]: 'Client',
  [MORPH_MAP.CONTACT]: 'Contact'
} as const;

export const getMorphMapModel = (morphMap: keyof typeof MORPH_MAP): string => {
  return MORPH_MAP_MODELS[morphMap] || 'Unknown';
};

export type MorphMapValue = typeof MORPH_MAP[keyof typeof MORPH_MAP];
```

### Using in Your Frontend

[](#using-in-your-frontend)

```
import { MORPH_MAP, getMorphMapModel } 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
--------

[](#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
---------------

[](#command-options)

OptionDescriptionDefault`--ts`Generate TypeScript instead of JavaScript`false``--path`Custom output path for generated file`resources/js`Best Practices
--------------

[](#best-practices)

1. **Version Control**: Add the generated file to your version control system to track changes.
2. **Build Process**: Include the generation command in your build process to ensure sync:

```
{
  "scripts": {
    "prepare": "php artisan morphmap:generate-js --ts"
  }
}
```

3. **Type Safety**: Use TypeScript for better type checking and IDE support.

Security
--------

[](#security)

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

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

43

—

FairBetter than 89% of packages

Maintenance88

Actively maintained with recent releases

Popularity16

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 63.6% 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 ~184 days

Total

4

Last Release

59d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6b8b982f1ca65bca2356c040bc612db0d03ee2443fd194bdfa9f44a4c3fa2267?d=identicon)[gigerit](/maintainers/gigerit)

---

Top Contributors

[![gigerIT](https://avatars.githubusercontent.com/u/64487711?v=4)](https://github.com/gigerIT "gigerIT (7 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (4 commits)")

---

Tags

laraveljavascripttypescriptconstantsfrontendmorphmap

### Embed Badge

![Health badge](/badges/gigerit-laravel-morphmap-js-generator/health.svg)

```
[![Health](https://phpackages.com/badges/gigerit-laravel-morphmap-js-generator/health.svg)](https://phpackages.com/packages/gigerit-laravel-morphmap-js-generator)
```

###  Alternatives

[tightenco/ziggy

Use your Laravel named routes in JavaScript.

4.3k48.3M329](/packages/tightenco-ziggy)[firefly-iii/data-importer

Firefly III Data Import Tool.

8035.8k](/packages/firefly-iii-data-importer)[markwalet/nova-modal-response

A Laravel Nova asset for Modal responses on an action.

17878.9k](/packages/markwalet-nova-modal-response)[ronasit/laravel-helpers

Provided helpers function and some helper class.

2085.6k31](/packages/ronasit-laravel-helpers)[team-nifty-gmbh/tall-datatables

Server-side rendered datatables for Laravel and Livewire

1320.9k4](/packages/team-nifty-gmbh-tall-datatables)[tomshaw/electricgrid

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

119.4k](/packages/tomshaw-electricgrid)

PHPackages © 2026

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