### Install Model Typer Source: https://context7.com/fumeapp/modeltyper/llms.txt Install the package as a dev dependency using Composer. Optionally, publish the configuration file. ```bash composer require --dev fumeapp/modeltyper ``` ```bash php artisan vendor:publish --provider="FumeApp\ModelTyper\ModelTyperServiceProvider" --tag=config ``` -------------------------------- ### Example Default TypeScript Interface Source: https://github.com/fumeapp/modeltyper/blob/master/readme.md A minimal example of a generated TypeScript interface for a User model, demonstrating the default interface output format. ```typescript export interface User { id: number; name: string; email: string; } ``` -------------------------------- ### Install Model Typer with Composer Source: https://github.com/fumeapp/modeltyper/blob/master/readme.md Use this command to require the Model Typer package as a development dependency. ```bash composer require --dev fumeapp/modeltyper ``` -------------------------------- ### Example TypeScript Interface for Comment Model Source: https://github.com/fumeapp/modeltyper/blob/master/readme.md An example TypeScript interface for a Comment model, including its columns, mutators, and relations. ```typescript export interface Comment { // columns id: number; post_id: number; content: string; created_at?: Date; updated_at?: Date; // mutators summary: string; // relations post: Post; } export type Comments = Array; ``` -------------------------------- ### Example TypeScript Interface for User Model Source: https://github.com/fumeapp/modeltyper/blob/master/readme.md This is an example of a generated TypeScript interface for a User model, including columns, mutators, relations, and counts. Interfaces are generated by default. ```typescript export interface User { // columns id: number; email: string; name: string; created_at?: Date; updated_at?: Date; // mutators first_name: string; initials: string; // relations teams: Teams; posts: Posts; // counts teams_count: number; posts_count: number; // exists teams_exists: boolean; posts_exists: boolean; } export type Users = Array; ``` -------------------------------- ### Example TypeScript Interface for Team Model Source: https://github.com/fumeapp/modeltyper/blob/master/readme.md An example TypeScript interface for a Team model, showcasing generated properties for columns, mutators, relations, and counts. ```typescript export interface Team { // columns id: number; name: string; logo: string; created_at?: Date; updated_at?: Date; // mutators initials: string; slug: string; url: string; // relations users: Users; // counts users_count: number; // exists users_exists: boolean; } export type Teams = Array; ``` -------------------------------- ### Configure Sum Aggregates for Relationships Source: https://github.com/fumeapp/modeltyper/blob/master/readme.md Example of defining the $sums property on a model to configure sum aggregates for relationships. ```php protected $sums = [ // Format: 'relationship' => 'column_to_sum' 'posts' => 'likes', ]; ``` -------------------------------- ### Example TypeScript Interface for Post Model Source: https://github.com/fumeapp/modeltyper/blob/master/readme.md This TypeScript interface represents a Post model, detailing its columns, mutators, relations, counts, and sums. ```typescript export interface Post { // columns id: number; user_id: number; title: string; content: string; created_at?: Date; updated_at?: Date; // mutators summary: string; // relations user: User; comments: Comments; // counts comments_count: number; // exists comments_exists: boolean; // sums comments_sum_likes: number | null; } export type Posts = Array; ``` -------------------------------- ### Define Model Relationship Return Type Source: https://github.com/fumeapp/modeltyper/blob/master/readme.md Example of a model relationship method with a required return type declaration. ```php public function providers(): HasMany // <- this { return $this->hasMany(Provider::class); } ``` -------------------------------- ### Generated TypeScript Interface with Sum Properties Source: https://context7.com/fumeapp/modeltyper/llms.txt Example of a generated TypeScript interface for a User model, including properties for relationship counts and sum aggregates like `posts_sum_likes`. ```typescript // export interface User { // id: number // name: string // // relations // posts: Post[] // // counts // posts_count: number // // exists // posts_exists: boolean // // sums // posts_sum_likes: number | null // } ``` -------------------------------- ### Define Custom Point Interface for Location Model Source: https://github.com/fumeapp/modeltyper/blob/master/readme.md Specify custom interfaces for model properties using the `interfaces` array. This example shows how to define a `Point` interface for the `coordinate` property in a `Location` model. ```php public array $interfaces = [ 'coordinate' => [ 'import' => "@/types/api", 'type' => 'Point', ], ]; ``` ```typescript import { Point } from "@/types/api"; export interface Location { // override coordinate: Point; } ``` -------------------------------- ### Define TypeScript User Type Source: https://github.com/fumeapp/modeltyper/blob/master/readme.md Example of a TypeScript type definition for a User model. This is generated when the --use-types flag is enabled. ```typescript export type User = { id: number; name: string; email: string; }; ``` -------------------------------- ### Define Model Mutation Return Type Source: https://github.com/fumeapp/modeltyper/blob/master/readme.md Example of a model attribute mutator with a required return type declaration. ```php protected function firstName(): Attribute { return Attribute::make( get: fn (string $value): string => ucfirst($value), // <- this ); } ``` -------------------------------- ### Generated TypeScript Type for Relationship Sum Source: https://github.com/fumeapp/modeltyper/blob/master/readme.md Example of a generated TypeScript type reflecting a sum aggregate from a relationship. ```typescript export interface User { ... // Sum of `likes` from related `posts` posts_sum_likes: number | null; } ``` -------------------------------- ### Handle Enum Eloquent Attribute Casting Source: https://github.com/fumeapp/modeltyper/blob/master/readme.md Model Typer detects Laravel Enum casting and generates corresponding TypeScript object literals or enums. This example shows the output for a `UserRoleEnum` with comments. ```php App\Enums\UserRoleEnum::class, ]; ``` ```typescript const UserRoleEnum = { /** Can do anything */ ADMIN: 'admin', /** Standard read-only */ USER: 'user', } export type UseRoleEnum = typeof UserRoleEnum[keyof typeof UserRoleEnum] export interface User { ... role: UserRoleEnum ... } ``` -------------------------------- ### Publish new configuration file Source: https://github.com/fumeapp/modeltyper/blob/master/UPGRADE.md Run this command to regenerate the configuration file after deleting the existing one to include new options. ```bash php artisan vendor:publish --provider="FumeApp\ModelTyper\ModelTyperServiceProvider" --tag=config ``` -------------------------------- ### Enable All Output Options Source: https://github.com/fumeapp/modeltyper/blob/master/readme.md Combine the `--plurals` and `--api-resources` options by using the `--all` artisan command option. ```bash artisan model:typer --all ``` ```bash artisan model:typer --plurals --api-resources ``` -------------------------------- ### Output API Resource Interfaces Source: https://github.com/fumeapp/modeltyper/blob/master/readme.md Generate interfaces for API resources, extending `api.MetApiResults`, `api.MetApiData`, and `api.MetApiResponse`, using the `--api-resources` artisan command option. ```bash artisan model:typer --api-resources ``` ```typescript export interface UserResults extends api.MetApiResults { data: Users; } export interface UserResult extends api.MetApiResults { data: User; } export interface UserMetApiData extends api.MetApiData { data: User; } export interface UserResponse extends api.MetApiResponse { data: UserMetApiData; } ``` -------------------------------- ### Output Interfaces as JSON Source: https://github.com/fumeapp/modeltyper/blob/master/readme.md Generate model interfaces in JSON format using the `--json` artisan command option. ```bash artisan model:typer --json ``` -------------------------------- ### View Type Mappings Source: https://context7.com/fumeapp/modeltyper/llms.txt Displays the current PHP-to-TypeScript type conversion table, including defaults and custom mappings. Use `--timestamps-date` to see date mappings. ```bash # Show current type mappings php artisan model:typer-mappings ``` ```bash # Show mappings with timestamps as Date php artisan model:typer-mappings --timestamps-date ``` -------------------------------- ### Generate API Resource Interfaces Source: https://context7.com/fumeapp/modeltyper/llms.txt Generate MetApi-compatible interfaces for standardized API responses using the --api-resources option. This creates interfaces for data, results, and response variations. ```bash php artisan model:typer --api-resources ``` ```typescript export interface User { id: number name: string } export interface UserResult extends api.MetApiResults { data: User } export interface UserMetApiData extends api.MetApiData { data: User } export interface UserResponse extends api.MetApiResponse { data: User } ``` -------------------------------- ### Declare Global Interfaces Source: https://github.com/fumeapp/modeltyper/blob/master/readme.md Generate interfaces in a global namespace named `model` using the `--global` artisan command option. ```bash artisan model:typer --global ``` ```typescript export {} declare global { export namespace models { export interface Provider { // columns id: number user_id: number avatar?: string ... ``` -------------------------------- ### Generate Interfaces for a Single Model Source: https://github.com/fumeapp/modeltyper/blob/master/readme.md Generate interfaces for a specific model by providing the model name to the `--model` artisan command option. ```bash artisan model:typer --model=User ``` -------------------------------- ### Define Custom Interface Without Import Source: https://github.com/fumeapp/modeltyper/blob/master/readme.md Use a custom interface directly without specifying an import path by providing the type as a string literal. ```php public array $interfaces = [ 'choices' => [ 'type' => "'good' | 'bad'", ], ]; ``` ```typescript export interface Location { // columns choices: "good" | "bad"; } ``` -------------------------------- ### View Current Model Typer Mappings Source: https://github.com/fumeapp/modeltyper/blob/master/readme.md This command displays the current mappings used by Model Typer for converting database types to TypeScript types. These mappings can be extended or overridden in the configuration file. ```bash php artisan model:typer-mappings ``` -------------------------------- ### Output Plural Interfaces for Collections Source: https://github.com/fumeapp/modeltyper/blob/master/readme.md Enable the generation of plural interfaces for model collections using the `--plurals` artisan command option. This creates a type alias for an array of the model. ```bash artisan model:typer --plurals ``` ```typescript export type Users = User[]; ``` -------------------------------- ### Generate TypeScript Interfaces Source: https://github.com/fumeapp/modeltyper/blob/master/readme.md Run this Artisan command to generate TypeScript interfaces from your Laravel models. The output is a type-safe representation of your models. ```bash php artisan model:typer ``` -------------------------------- ### Configure Custom Type Mappings Source: https://context7.com/fumeapp/modeltyper/llms.txt Extend default PHP-to-TypeScript mappings by defining custom casts, overriding built-in types, or mapping specific database types in the `custom_mappings` configuration. ```php [ // Map custom casts to TypeScript types 'App\Casts\MoneyCast' => 'number', 'App\Casts\PointCast' => '{ lat: number, lng: number }', // Override built-in types 'binary' => 'Blob', 'year' => 'string', // Map specific database types 'point' => 'GeoPoint', 'geometry' => 'GeoJSON', ], // Define custom relationship methods from packages 'custom_relationships' => [ 'singular' => [ 'belongsToThrough', // staudenmeir/belongs-to-through ], 'plural' => [ 'hasManyThrough', ], ], ]; ``` -------------------------------- ### Configure Model Typer Output Options Source: https://context7.com/fumeapp/modeltyper/llms.txt Control various aspects of TypeScript generation, including automatic regeneration after migrations, output file settings, namespace usage, and formatting options for attributes and relationships. ```php true, // Auto-output to file instead of console 'output-file' => true, 'output-file-path' => './resources/js/types/models.d.ts', // Use global namespace 'global' => false, 'global-namespace' => 'models', // Output format options 'json' => false, 'use-enums' => false, 'use-types' => false, 'plurals' => true, 'api-resources' => false, // Relationship options 'no-relations' => false, 'optional-relations' => false, 'no-counts' => false, 'optional-counts' => true, 'no-exists' => false, 'optional-exists' => true, 'no-sums' => false, 'optional-sums' => true, // Attribute options 'no-hidden' => true, 'timestamps-date' => false, 'optional-nullables' => false, 'fillables' => true, 'fillable-suffix' => 'Fillable', // Case conversion for output 'case' => [ 'columns' => 'snake', // snake, camel, or pascal 'relations' => 'snake', ], // Model filtering 'included_models' => [ // Only generate for these models (empty = all) ], 'excluded_models' => [ 'App\Models\Pivot', 'App\Models\BaseModel', ], // Additional paths to scan for models 'additional_paths' => [ './packages/my-package/src/Models', ], ]; ``` -------------------------------- ### Generate JSON Output Source: https://context7.com/fumeapp/modeltyper/llms.txt Output model definitions as JSON using the --json flag. This is useful for integration with other tools or custom processing. ```bash php artisan model:typer --json ``` ```json { "interfaces": { "User": [ { "name": "id", "type": "number" }, { "name": "name", "type": "string" }, { "name": "email", "type": "string" } ] }, "relations": [...], "enums": [...] } ``` -------------------------------- ### Generate Fillable Types Source: https://context7.com/fumeapp/modeltyper/llms.txt Generates a utility type containing only the model's fillable attributes. Use `--fillable-suffix` to customize the output type name. ```bash # Generate fillable types php artisan model:typer --fillables ``` ```bash # With custom suffix php artisan model:typer --fillables --fillable-suffix=Input ``` -------------------------------- ### Enable Types for Specific Model via CLI Source: https://github.com/fumeapp/modeltyper/blob/master/readme.md Command to enable TypeScript type generation for a specific model using the CLI option. ```bash php artisan model:typer --model=User --use-types ``` -------------------------------- ### Define Nullable Custom Interface Source: https://github.com/fumeapp/modeltyper/blob/master/readme.md Make a custom interface nullable by setting the `nullable` option to `true` within the `interfaces` array. ```php public array $interfaces = [ 'choices' => [ 'import' => '@/types/api', 'type' => 'ChoicesWithPivot', 'nullable' => true, ], ]; ``` -------------------------------- ### Override Default Mappings and Add New Ones Source: https://github.com/fumeapp/modeltyper/blob/master/readme.md Customize type mappings by editing the `custom_mappings` array in the configuration file. This allows overriding default mappings or adding new ones for custom casts. ```php 'custom_mappings' => [ 'App\Casts\YourCustomCast' => 'string | null', 'binary' => 'Blob', 'bool' => 'boolean', 'point' => 'CustomPointInterface', 'year' => 'string', ], ``` -------------------------------- ### Generate Global Namespace Declarations Source: https://context7.com/fumeapp/modeltyper/llms.txt Wrap all generated interfaces in a TypeScript namespace using the --global option to avoid naming conflicts. The default namespace is 'models'. ```bash php artisan model:typer --global ``` ```typescript export {} declare global { export namespace models { export interface User { id: number name: string email: string } } } ``` -------------------------------- ### Make Nullable Attributes Optional Source: https://context7.com/fumeapp/modeltyper/llms.txt Makes nullable database columns optional in the generated TypeScript interfaces (e.g., `field?: type` instead of `field: type | null`). This simplifies handling optional fields. ```bash # Make nullable attributes optional (field?: type instead of field: type | null) php artisan model:typer --optional-nullables ``` -------------------------------- ### Generate TypeScript Types Source: https://github.com/fumeapp/modeltyper/blob/master/readme.md Use the --use-types flag with the model:typer command to generate TypeScript type aliases instead of interfaces. ```bash php artisan model:typer --use-types ``` -------------------------------- ### Generate Plural Collection Types Source: https://context7.com/fumeapp/modeltyper/llms.txt Create additional type aliases representing arrays of each model using the --plurals option. This is useful for API responses that return collections. ```bash php artisan model:typer --plurals ``` ```typescript export interface User { id: number name: string } export type Users = User[] ``` -------------------------------- ### Defining Typed Eloquent Models Source: https://context7.com/fumeapp/modeltyper/llms.txt Ensure all relationships and accessors include explicit return type declarations to allow Model Typer to infer the correct TypeScript types. ```php // app/Models/User.php 'datetime', 'role' => Role::class, ]; // Relationships MUST have return type declarations public function posts(): HasMany { return $this->hasMany(Post::class); } public function team(): BelongsTo { return $this->belongsTo(Team::class); } // New-style accessors MUST have return type in closure protected function firstName(): Attribute { return Attribute::make( get: fn (string $value): string => ucfirst($value), ); } // Traditional accessors MUST have return type public function getFullNameAttribute(): string { return "{$this->first_name} {$this->last_name}"; } // Union types are supported protected function score(): Attribute { return Attribute::get(fn (): float|int => 42); } // Nullable union types protected function nickname(): Attribute { return Attribute::get(fn (): ?string => $this->attributes['nickname'] ?? null); } } ``` -------------------------------- ### Append Additional Properties to Custom Interface Source: https://github.com/fumeapp/modeltyper/blob/master/readme.md Add extra properties to a model's interface, such as state management properties, by defining them within the `interfaces` array. ```php public array $interfaces = [ 'state' => [ 'type' => "found' | 'not_found' | 'searching' | 'reset'", ], ]; ``` ```typescript export interface Location { // ... // overrides state: "found" | "not_found" | "searching" | "reset"; // ... } ``` -------------------------------- ### Make Relationships Optional Source: https://context7.com/fumeapp/modeltyper/llms.txt Makes all relationships optional in the generated TypeScript interfaces, allowing them to be `undefined`. Use this when relationships might not always be loaded. ```bash # Make relationships optional (User | undefined instead of User) php artisan model:typer --optional-relations ``` -------------------------------- ### Define Sum Aggregates for Relationships Source: https://context7.com/fumeapp/modeltyper/llms.txt Enable the generation of TypeScript properties for relationship sum calculations by defining the `$sums` property on your Eloquent models. This is commonly used with the `withSum()` method. ```php 'likes', // posts_sum_likes 'comments' => 'votes', // comments_sum_votes ]; public function posts(): HasMany { return $this->hasMany(Post::class); } } ``` -------------------------------- ### Output Timestamps as Date Type Source: https://context7.com/fumeapp/modeltyper/llms.txt Outputs timestamp columns (like `created_at`, `updated_at`) as the `Date` type in TypeScript instead of `string`. This provides better type safety for date manipulation. ```bash # Output timestamps as Date type instead of string php artisan model:typer --timestamps-date ``` -------------------------------- ### Generate TypeScript Types Source: https://context7.com/fumeapp/modeltyper/llms.txt Use the --use-types flag to generate TypeScript type aliases instead of interfaces. Type aliases are useful for functional programming patterns or complex type definitions. ```bash php artisan model:typer --use-types ``` ```typescript export type User = { id: number name: string email: string } ``` -------------------------------- ### Enum Casting Support Source: https://context7.com/fumeapp/modeltyper/llms.txt Automatically converts Laravel enum casts to TypeScript const objects with preserved documentation. Use `--use-enums` to generate TypeScript enums instead of const objects. ```php // app/Enums/UserRoleEnum.php UserRoleEnum::class, ]; ``` ```bash # Generated TypeScript output: php artisan model:typer # const UserRoleEnum = { # /** Can do anything */ # ADMIN: 'admin', # /** Standard read-only */ # USER: 'user', # } as const; # export type UserRoleEnum = typeof UserRoleEnum[keyof typeof UserRoleEnum] # # export interface User { # role: UserRoleEnum # } # Use --use-enums for TypeScript enums instead php artisan model:typer --use-enums ``` -------------------------------- ### Make Relationship Exists Properties Optional Source: https://context7.com/fumeapp/modeltyper/llms.txt Makes relationship `exists` properties optional in the generated TypeScript interfaces. This is useful if the existence check might not always be available. ```bash # Make exists optional php artisan model:typer --optional-exists ``` -------------------------------- ### Make Relationship Counts Optional Source: https://context7.com/fumeapp/modeltyper/llms.txt Makes relationship count properties optional in the generated TypeScript interfaces. This is useful if counts are not always present. ```bash # Make counts optional php artisan model:typer --optional-counts ``` -------------------------------- ### Exclude Relationship Exists Properties Source: https://context7.com/fumeapp/modeltyper/llms.txt Excludes relationship `exists` properties (e.g., `has_posts`) from the generated TypeScript interfaces. Use this when you don't need to check for the existence of related models. ```bash # Exclude exists properties php artisan model:typer --no-exists ``` -------------------------------- ### Exclude Relationships Source: https://context7.com/fumeapp/modeltyper/llms.txt Excludes all relationships from the generated TypeScript interfaces. This is useful when relationships are not needed in the frontend. ```bash # Exclude relationships entirely php artisan model:typer --no-relations ``` -------------------------------- ### Exclude Relationship Counts Source: https://context7.com/fumeapp/modeltyper/llms.txt Excludes relationship count properties (e.g., `user_count`) from the generated TypeScript interfaces. Use this if you don't need to display counts. ```bash # Exclude relationship counts php artisan model:typer --no-counts ``` -------------------------------- ### Make Relationship Sum Aggregates Optional Source: https://context7.com/fumeapp/modeltyper/llms.txt Makes relationship sum aggregate properties optional in the generated TypeScript interfaces. This is useful when sums might not always be calculated or available. ```bash # Make sums optional php artisan model:typer --optional-sums ``` -------------------------------- ### Exclude Relationship Sum Aggregates Source: https://context7.com/fumeapp/modeltyper/llms.txt Excludes relationship sum aggregate properties from the generated TypeScript interfaces. Use this if you do not need to display sums of related data. ```bash # Exclude sum aggregates php artisan model:typer --no-sums ``` -------------------------------- ### Custom Interface Overrides Source: https://context7.com/fumeapp/modeltyper/llms.txt Overrides generated types for specific model attributes using the `$interfaces` property. Define custom types, imports, and nullability for columns, mutators, or relationships. ```php // app/Models/Location.php [ 'import' => '@/types/api', 'type' => 'Point', ], // Nullable custom type 'choices' => [ 'import' => '@/types/api', 'type' => 'ChoicesWithPivot', 'nullable' => true, ], // Inline union type without import 'status' => [ 'type' => "'active' | 'inactive' | 'pending'", ], ]; } ``` ```bash # Generated output includes import and custom type: php artisan model:typer ``` -------------------------------- ### Exclude Hidden Attributes Source: https://context7.com/fumeapp/modeltyper/llms.txt Excludes hidden model attributes (like passwords and tokens) from the generated TypeScript interfaces. This enhances security by not exposing sensitive data. ```bash # Exclude hidden attributes (password, remember_token, etc.) php artisan model:typer --no-hidden ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.