Repo of the Day
fumeapp/modeltyper
Published: Jul 6, 2022
Open repository ↗Summary
Model Typer is a Composer package for Laravel that reads Eloquent models and emits matching TypeScript declarations for the front end. It supports columns, mutators, relations, counts, exists flags, and sum aggregates, and can write interfaces, type aliases, or JSON to a file. The README positions it as a stable, focused alternative to Laravel Wayfinder's model support, which is currently in a dev-next branch.
What it is useful for
It is useful when a Laravel backend feeds a TypeScript front end (React, Vue, Inertia, etc.) and you want shared type definitions instead of hand-written any types. It covers things that are tedious to keep in sync by hand: database columns, accessor/mutator outputs, Eloquent relations, and the _count / _exists columns returned by eager-loaded queries. PHP enum casts are also recognised, and the generator can emit them as object literals, const enum, or plain enum depending on flags.
A few constraints from the README: model relationships and mutators need explicit return types, and the current major version targets Laravel 11.33+ on PHP 8.2+. Laravel 10.x users are directed to pin to v2.
How engineers can use it
Install as a dev dependency:
composer require --dev fumeapp/modeltyper
Optionally publish the config so you can tweak mappings and output options:
php artisan vendor:publish --provider="FumeApp\ModelTyper\ModelTyperServiceProvider" --tag=config
Then run the generator, optionally writing to a file:
php artisan model:typer --output-file=resources/js/types/models.d.ts
Useful flags listed in the README include --model=User to target one model, --use-types for type aliases instead of interfaces, --plurals to add Users = User[], --api-resources for MetApi response shapes, --no-relations to strip relations, and --json for JSON output. For sum aggregates, add a $sums array on the model mapping a relationship to a column (e.g. 'posts' => 'likes'), which produces a posts_sum_likes: number | null field. Custom or third-party column types can be wired through custom_mappings in the published config, and per-model overrides go in a public $interfaces array on the Eloquent model. The README is the source of truth for the full flag list and a migration note in UPGRADE.md.