PHPackages                             oi-lab/oi-laravel-ts - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. oi-lab/oi-laravel-ts

ActiveLibrary[Database &amp; ORM](/categories/database)

oi-lab/oi-laravel-ts
====================

Generate TypeScript interfaces from Laravel Eloquent models

v1.0.2(1mo ago)194MITPHPPHP ^8.2CI failing

Since Oct 30Pushed 1mo agoCompare

[ Source](https://github.com/oi-lab/oi-laravel-ts)[ Packagist](https://packagist.org/packages/oi-lab/oi-laravel-ts)[ RSS](/packages/oi-lab-oi-laravel-ts/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (12)Versions (4)Used By (0)

OI Laravel TypeScript Generator
===============================

[](#oi-laravel-typescript-generator)

[![Latest Version on Packagist](https://camo.githubusercontent.com/db36fb4f79bc8243d4cb61f18a2107ea85b11835bbb073086d588f30393cd8a7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6f692d6c61622f6f692d6c61726176656c2d74732e737667)](https://packagist.org/packages/oi-lab/oi-laravel-ts)[![Total Downloads](https://camo.githubusercontent.com/86456ffc6cc59695c0b7a9cf4b1f4919bd9e0c3e7b6a9e1b3bfc491d8c5d9fd1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6f692d6c61622f6f692d6c61726176656c2d74732e737667)](https://packagist.org/packages/oi-lab/oi-laravel-ts)[![Tests](https://camo.githubusercontent.com/328a59644a055d910db59ec07ad1dc55d89d55c73e9da030a6e5e0b7253c94c1/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6f692d6c61622f6f692d6c61726176656c2d74732f74657374732e796d6c3f6c6162656c3d7465737473)](https://github.com/oi-lab/oi-laravel-ts/actions)[![License](https://camo.githubusercontent.com/50bd97e422fd2bd48b6396a0b85ae534cf8970357c8562a622fa6fbb374abaa6/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6f692d6c61622f6f692d6c61726176656c2d7473)](LICENSE)

A Laravel package that automatically generates TypeScript interfaces from your Eloquent models, complete with relationships, custom casts, and DataObjects support.

Features
--------

[](#features)

- **Automatic Interface Generation**: Converts Eloquent models to TypeScript interfaces
- **Relationship Support**: Handles all Laravel relationship types (HasOne, HasMany, BelongsTo, etc.)
- **Custom Casts**: Supports Laravel custom casts and automatically detects DataObjects
- **PHPDoc Support**: Reads PHPDoc annotations for complex types
- **Watch Mode**: Monitor your models directory and regenerate on changes
- **Configurable**: Extensive configuration options for customization
- **JSON-LD Support**: Optional support for JSON-LD data structures

Architecture
------------

[](#architecture)

This package uses a modular architecture with clear separation of concerns, organized in two main pipelines:

### Pipeline 1: Eloquent Analysis

[](#pipeline-1-eloquent-analysis)

- **Eloquent**: Facade for model analysis and schema generation
- **ModelDiscovery**: Discovers all Eloquent models in the application
- **TypeExtractor**: Extracts type information from models
- **CastTypeResolver**: Resolves custom Laravel casts to TypeScript types
- **RelationshipResolver**: Detects and extracts relationship metadata
- **DataObjectAnalyzer**: Analyzes PHP DataObject classes
- **PhpToTypeScriptConverter**: Converts PHP types to TypeScript
- **SchemaBuilder**: Orchestrates schema building for all models

### Pipeline 2: TypeScript Generation

[](#pipeline-2-typescript-generation)

- **Convert**: Main orchestrator coordinating the conversion process
- **TypeScriptTypeConverter**: Handles schema to TypeScript type conversion
- **DataObjectProcessor**: Processes PHP DataObjects and generates their interfaces
- **ModelInterfaceGenerator**: Generates TypeScript interfaces for Laravel models
- **ImportManager**: Manages TypeScript import statements
- **JsonLdGenerator**: Generates JSON-LD support interfaces

For detailed architecture documentation, see [ARCHITECTURE.md](ARCHITECTURE.md).

Requirements
------------

[](#requirements)

- PHP 8.2+
- Laravel 11.0+, 12.0+, or 13.0+

Installation
------------

[](#installation)

```
composer require oi-lab/oi-laravel-ts
```

### Local Development

[](#local-development)

For local development, add this to your main project's `composer.json`:

```
{
    "repositories": [
        {
            "type": "path",
            "url": "./packages/oi-lab/oi-laravel-ts"
        }
    ]
}
```

Then:

```
composer require oi-lab/oi-laravel-ts
```

Configuration
-------------

[](#configuration)

Publish the configuration file:

```
php artisan vendor:publish --tag=oi-laravel-ts-config
```

This creates `config/oi-laravel-ts.php` with the following options:

```
return [
    // Output path for generated TypeScript file
    'output_path' => resource_path('js/types/interfaces.ts'),

    // Include _count fields for relationships
    'with_counts' => true,

    // Enable JSON-LD support
    'with_json_ld' => false,

    // Save intermediate schema.json for debugging
    'save_schema' => false,

    // Define specific types for model properties
    'props_with_types' => [],

    // Add custom properties to models
    'custom_props' => [
        'Organization' => [
            'uuid' => 'string',
        ],
    ],
];
```

Usage
-----

[](#usage)

### Basic Generation

[](#basic-generation)

Generate TypeScript interfaces from your models:

```
php artisan oi:gen-ts
```

This will scan all models in `app/Models` and generate a TypeScript file at the configured output path.

### Watch Mode

[](#watch-mode)

Automatically regenerate when models change:

```
php artisan oi:gen-ts --watch
```

### Example Output

[](#example-output)

Given a Laravel model:

```
