{"id":15984589,"url":"https://github.com/7nohe/laravel-typegen","last_synced_at":"2025-08-21T02:31:08.782Z","repository":{"id":63526259,"uuid":"568150882","full_name":"7nohe/laravel-typegen","owner":"7nohe","description":"The library lets you generate TypeScript types from your Laravel code","archived":false,"fork":false,"pushed_at":"2024-09-16T05:28:58.000Z","size":303,"stargazers_count":101,"open_issues_count":9,"forks_count":5,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-07-26T12:25:01.099Z","etag":null,"topics":["inertia","inertiajs","laravel","typegen","typescript","ziggy"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/7nohe.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":null,"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":["7nohe"]}},"created_at":"2022-11-19T16:00:07.000Z","updated_at":"2025-05-26T17:08:08.000Z","dependencies_parsed_at":"2024-05-21T14:51:34.364Z","dependency_job_id":null,"html_url":"https://github.com/7nohe/laravel-typegen","commit_stats":{"total_commits":33,"total_committers":2,"mean_commits":16.5,"dds":0.303030303030303,"last_synced_commit":"e4c27ba655a5ec5b3cfcd34adebf382b2f4f6902"},"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"purl":"pkg:github/7nohe/laravel-typegen","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/7nohe%2Flaravel-typegen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/7nohe%2Flaravel-typegen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/7nohe%2Flaravel-typegen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/7nohe%2Flaravel-typegen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/7nohe","download_url":"https://codeload.github.com/7nohe/laravel-typegen/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/7nohe%2Flaravel-typegen/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271416747,"owners_count":24755944,"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","status":"online","status_checked_at":"2025-08-21T02:00:08.990Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["inertia","inertiajs","laravel","typegen","typescript","ziggy"],"created_at":"2024-10-08T02:09:44.087Z","updated_at":"2025-08-21T02:31:08.446Z","avatar_url":"https://github.com/7nohe.png","language":"TypeScript","funding_links":["https://github.com/sponsors/7nohe"],"categories":[],"sub_categories":[],"readme":"# Laravel Typegen\n\n## Table of contents\n- [Features](#features)\n- [Supported Versions](#supported-versions)\n- [Installation](#installation)\n- [Usage](#usage)\n    - [Enum Support](#enum-support)\n    - [Laravel Enum Support](#laravel-enum-support)\n    - [Use strongly typed `route()` function for ziggy](#use-strongly-typed-route-function-for-ziggy)\n    - [Useful types for Laravel](#useful-types-for-laravel)\n    - [Form Request types](#form-request-types)\n- [Available Options](#available-options)\n- [Development](#development)\n    - [Setup example project](#setup-example-project)\n    - [Debug](#debug)\n- [License](#license)\n\n\n## Features\n\n- Generate types from Laravel's Models\n- Support Relationhips\n- Support Enum (from PHP8.1)\n- Generate types from Laravel's Form Requests\n- Generate types for ziggy (Routing)\n- Provide useful types for Laravel (e.g. pagination, etc.)\n\n## Supported Versions\nThis library supports the following versions:\n\n- Laravel 9.x and 10.x\n- TypeScript 5.0 and above\n\n## Installation\n\n```bash\n$ npm install -D @7nohe/laravel-typegen\n```\n\nLaravel Typegen may require installing `doctrine/dbal` via Composer in order to execute the `php artisan model:show` command internally.\n\n```bash\n$ composer require doctrine/dbal\n```\n\n## Usage\n\nEdit package.json\n```json\n{\n    \"scripts\": {\n        \"typegen\": \"laravel-typegen\"\n    },\n}\n```\n\n```bash\n$ npm run typegen\n```\n\n### Enum Support\n\nWe also support php8.1 enums.\n\n```php\n\u003c!-- app/Enums/GenderType.php --\u003e\n\u003c?php\n\nnamespace App\\Enums;\n\nenum GenderType: string\n{\n    case Male = 'Male';\n    case Female = 'Female';\n    case Other = 'Other';\n}\n```\n\nThen, cast model attributes to enums.\n\n```php\n\u003c!-- app/Models/User.php --\u003e\n\u003c?php\n\nnamespace App\\Models;\nuse App\\Enums\\GenderType;\n\nclass User extends Authenticatable\n{\n    /**\n     * The attributes that should be cast.\n     *\n     * @var array\u003cstring, string\u003e\n     */\n    protected $casts = [\n        'gender'            =\u003e GenderType::class,\n    ];\n}\n```\n\nThis library will generate the following TypeScript types:\n\n```typescript\nexport type User = {\n    id: number;\n    name: string;\n    email: string;\n    gender: GenderType;\n    email_verified_at?: string;\n    created_at?: string;\n    updated_at?: string;\n    posts?: Post[];\n};\nexport enum GenderType {\n    Male = \"Male\",\n    Female = \"Female\",\n    Other = \"Other\"\n}\n```\n\n### Laravel Enum Support\nIf you use [Laravel Enum](https://github.com/BenSampo/laravel-enum), use the option `--laravel-enum`.\n\n\n```json\n{\n    \"scripts\": {\n        \"typegen\": \"laravel-typegen --laravel-enum\"\n    },\n}\n```\n\n### Use strongly typed `route()` function for ziggy\n\nRunning the `laravel-typegen` command with the `--ziggy` option will generate route.d.ts.\n\nIt helps typing the `route()` function.\n\n```json\n{\n    \"scripts\": {\n        \"typegen\": \"laravel-typegen --ziggy\"\n    },\n}\n```\n\nFor example, define the following routes\n\n```php\n// routes/web.php\nRoute::resource('posts', PostsController::class);\n```\n\n```bash\n$ php artisan route:list\nGET|HEAD posts/{post} posts.show › PostsController@show\n```\n\nParameters will be checked strictly based on the route name.\n\n```ts\n// in your TypeScript code\n\n// OK\nroute('posts.show', { post: post.id })\n\n// Error\nroute('posts.show', { id: post.id })\n```\n\nIf you have created a project using the `--typescript` option of Laravel Breeze, you need to delete the declaration for the `route()` function in resources/js/types/global.d.ts.\n\n```diff\nimport { PageProps as InertiaPageProps } from '@inertiajs/core';\nimport { AxiosInstance } from 'axios';\n import ziggyRoute, { Config as ZiggyConfig } from 'ziggy-js';\nimport { PageProps as AppPageProps } from './';\n\ndeclare global {\n    interface Window {\n        axios: AxiosInstance;\n    }\n\n    var route: typeof ziggyRoute;\n    var Ziggy: ZiggyConfig;\n}\n\n- declare module 'vue' {\n-     interface ComponentCustomProperties {\n-        route: typeof ziggyRoute;\n-     }\n- }\n\ndeclare module '@inertiajs/core' {\n    interface PageProps extends InertiaPageProps, AppPageProps {}\n}\n```\n\n### Useful types for Laravel\n\nWe provide useful types for Laravel (especially for Inertia).\n\nFor example, to return a paginated Inertia response in DashboardController, you can write the following\n\n```php\n\u003c?php\n\nnamespace App\\Http\\Controllers;\n\nuse App\\Models\\User;\nuse Inertia\\Inertia;\n\nclass DashboardController extends Controller\n{\n    public function __invoke()\n    {\n        $users = User::latest('id')-\u003epaginate(5);\n        return Inertia::render(\n            'Dashboard',\n            [\n                'users' =\u003e $users\n            ]\n        );\n    }\n}\n\n```\n\nYou can import types already defined by Laravel Typegen.\n\n```vue\n\u003c!-- Dashboard.vue --\u003e\n\u003cscript setup lang=\"ts\"\u003e\nimport { Paginate } from '@7nohe/laravel-typegen';\nimport { User } from '@/types/model'; // generated types\n\ndefineProps\u003c{ users: Paginate\u003cUser\u003e }\u003e();\n\n\u003c/script\u003e\n\u003ctemplate\u003e\n    \u003cdiv\u003e\n        \u003cul\u003e\n            \u003cli v-for=\"user in users.data\"\u003e- {{ user.name }}({{ user.email }})\u003c/li\u003e\n        \u003c/ul\u003e\n        \u003cdiv class=\"flex justify-center mt-4 space-x-4\"\u003e\n            \u003cLink v-for=\"(link, key) in users.links\" :key=\"key\" :href=\"link.url ?? '#'\" v-html=\"link.label\" /\u003e\n        \u003c/div\u003e\n    \u003c/div\u003e\n\u003c/template\u003e\n```\n\n### Form Request types\nYou can generate types from Laravel's Form Request by executing the command with the `--form-requests` option.\n\nIf you have the Form Request class as shown below:\n\n```php\n\u003c?php\n\nnamespace App\\Http\\Requests;\n\nuse App\\Models\\User;\nuse Illuminate\\Foundation\\Http\\FormRequest;\nuse Illuminate\\Validation\\Rule;\n\nclass ProfileUpdateRequest extends FormRequest\n{\n    /**\n     * Get the validation rules that apply to the request.\n     *\n     * @return array\u003cstring, mixed\u003e\n     */\n    public function rules()\n    {\n        return [\n            'name' =\u003e ['string', 'max:255'],\n            'email' =\u003e ['email', 'max:255', Rule::unique(User::class)-\u003eignore($this-\u003euser()-\u003eid)],\n        ];\n    }\n}\n```\n\nThe corresponding TypeScript types will be automatically generated as follows:\n\n```ts\n// formRequests.ts\nexport type ProfileUpdateRequest = {\n    name?: string;\n    email?: string;\n};\n```\n\nBy using these generated types in combination with HTTP Client libraries like Axios, you can write more type-safe code.\n\n## Available options\n\n```bash\nUsage: laravel-typegen [options]\n\nGenerate TypeScript types from your Laravel code\n\nOptions:\n  -V, --version                output the version number\n  -o, --output \u003cvalue\u003e         Output directory (default: \"resources/js/types\")\n  --laravel-enum               Use Laravel Enum (default: false)\n  --enum-path \u003cvalue\u003e          Path to enum files (default: \"app/Enums\")\n  --model-path \u003cvalue\u003e         Path to model files (default: \"app/Models\")\n  -z, --ziggy                  Generate types for ziggy (default: false)\n  --vendor-routes              Include routes defined by vendor packages (default: false)\n  --ignore-route-dts           Ignore generating route.d.ts (default: false)\n  --form-request               Generate types for FormRequests (default: false)\n  --form-request-path \u003cvalue\u003e  Path to FormRequest files (default: \"app/Http/Requests\")\n  -h, --help                   display help for command\n```\n\n\n## Development\n\n### Setup example project\n\n```bash\n$ cd examples/laravel10-app\n$ cp .env.example .env\n$ docker run --rm \\\n    -u \"$(id -u):$(id -g)\" \\\n    -v \"$(pwd):/var/www/html\" \\\n    -w /var/www/html \\\n    laravelsail/php81-composer:latest \\\n    composer install --ignore-platform-reqs\n$ ./vendor/bin/sail up -d\n$ ./vendor/bin/sail php artisan key:generate\n$ ./vendor/bin/sail php artisan migrate --seed\n$ ./vendor/bin/sail npm install\n```\n\n### Debug\n\n```bash\n$ pnpm install\n$ sh debug.sh\n```\n\n## License\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F7nohe%2Flaravel-typegen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F7nohe%2Flaravel-typegen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F7nohe%2Flaravel-typegen/lists"}