Eloquent Models
Laravel TypeGen inspects your Eloquent models to generate precise TypeScript interfaces. It automatically maps database column types, model casts, timestamps, hidden attributes, and relationships.
Column Mapping & Casts
The generator maps Eloquent attributes using the following precedence rules:
- Primary Key: Identified by
getKeyName(). IfgetKeyType()isint, it maps tonumber, otherwisestring. - Casts: Attributes listed in
$casts(orcasts()) are mapped based on their cast type (e.g.booleantoboolean,integertonumber). - Fillable attributes: Columns listed in
$fillablethat are not cast are assumed to be of typestring. - Timestamps: If
$timestampsis enabled,created_atandupdated_at(or custom columns) are automatically generated asstring.
Customizing Names
By default, the interface name matches the PHP class name. You can customize the name of the exported interface by passing a parameter to the attribute:
#[TypeScript(name: 'AdminUser')]
class User extends Model {}This will output:
export interface AdminUser { ... }Hidden Attributes
Attributes listed in $hidden are omitted from the generated TS interface by default. You can change this behavior in config/typegen.php by setting 'include_hidden' => true.
Relationships
TypeGen supports auto-generation of Eloquent relationships. You must explicitly opt-in to relations on each model using the includeRelations parameter:
#[TypeScript(includeRelations: ['posts', 'profile'])]
class User extends Model
{
public function posts(): HasMany
{
return $this->hasMany(Post::class);
}
public function profile(): HasOne
{
return $this->hasOne(Profile::class);
}
}Auto-Discovery
When you include relationships, TypeGen uses a Breadth-First Search (BFS) graph walk to auto-discover and generate the related models (Post and Profile), even if they aren't marked with the #[TypeScript] attribute.
Generated types are automatically marked optional (?) with | null matching the runtime database nullable state:
export interface User {
id: number;
posts?: Post[];
profile?: Profile | null;
}Polymorphic Relations
Polymorphic MorphTo relationships are supported out of the box when a morph map is registered:
// AppServiceProvider.php
Relation::enforceMorphMap([
'post' => Post::class,
'video' => Video::class,
]);Which yields:
export interface Comment {
commentable?: (Post | Video) | null;
}If no morph map is registered, the relation defaults to unknown | null.