adrianvm / export-entities
Export Laravel model constants and enums to JavaScript/TypeScript for seamless backend-frontend sharing.
dev-main
2024-12-07 10:45 UTC
Requires
- php: >=8.0.2 < 9.0.0
- laravel/framework: >=9.0.0 < 12.0.0
This package is auto-updated.
Last update: 2025-07-07 12:00:33 UTC
README
Export PHP constants and/or PHP enums to JavaScript/TypeScript for seamless backend-frontend sharing.
Requirements
- PHP 8.0 or above (8.1 or above for Enums command to work)
- Laravel 9 or above
Installation
composer require --dev adrianvm/export-entities
Usage
This package adds two new commands:
1. Export constants
php artisan export:constants
1.1. Options
--suffix
: Append a custom suffix (optional, no default).--path
: Models folder path (default:Models
)output
: The output file path for JavaScript constants (default:resources/js/constants.js
)--typescript
: Generate TypeScript definitions.
1.2. Input Example
... class Post extends Model { ... const STATUS_DRAFT = 'draft'; const STATUS_PUBLISHED = 'published'; ... const TYPE_PRIVATE = 0; const TYPE_PUBLIC = 1; ... }
1.3. Output Example
export const Post = { STATUS_DRAFT: "draft", STATUS_PUBLISHED: "published", TYPE_PRIVATE: 0, TYPE_PUBLIC: 1 };
2. Export enums
php artisan export:enums
2.1. Options
--suffix
: Append a custom suffix (optional, no default).--path
: Enums folder path (default:Enums
)output
: The output file path for JavaScript constants (default:resources/js/enums.js
)--typescript
: Generate TypeScript definitions.
2.2.1. Input Example (no type)
... enum BurgerCookingStages { case Raw; case Medium; case WellDone; case Congratulations; }
2.2.1. Output Example (no type)
export const Post = { Raw: 'raw', Medium: 'medium', WellDone: 'well_done', Congratulations: 'congratulations' };
2.2.2. Input Example (string type)
... enum Status: string { case DRAFT = 'draft'; case PUBLISHED = 'published'; case ARCHIVED = 'archived'; case DELETED = 'is_deleted'; }
2.2.2. Output Example (string type)
export const Status = { DRAFT: 'draft', PUBLISHED: 'published', ARCHIVED: 'archived', DELETED: 'is_deleted' };
2.2.3. Input Example (int type)
... enum Status: int { case DRAFT = 1; case PUBLISHED = 2; case ARCHIVED = 3; }
2.2.3. Output Example (int type)
export const Status = { DRAFT: 1, PUBLISHED: 2, ARCHIVED: 3 };
License
This package is open-source software licensed under the MIT license.