1. Go to this page and download the library: Download oi-lab/oi-laravel-ts library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
oi-lab / oi-laravel-ts example snippets
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,
// Follow relationships to generate interfaces for referenced models
// (incl. models attached through traits, e.g. spatie/laravel-permission)
'discover_related_models' => true,
// Add every Eloquent model of these namespaces to the schema, as if in app/Models
'Name} interfaces
'extended_namespaces' => [],
// Save intermediate schema.json for debugging
'save_schema' => false,
// Namespaces holding spatie/laravel-data style DTOs to emit as I{ClassName}
'data_namespaces' => [],
// Give a DTO a distinct interface name when two share a short class name
'data_aliases' => [],
// Emit a DTO as a discriminated union (see "Discriminated Unions")
'data_discriminators' => [],
// 'null': `?` means "key may be absent", `| null` means "value may be null"
// 'optional': legacy — nullable or defaulted properties all render as `?`
'data_nullable_style' => 'null',
// When true, a model mapped to a DTO no longer emits its own Eloquent interface
'data_replaces_model' => false,
// Explicit model => DTO map (otherwise inferred from the DTO's fromModel() factory)
'data_for_model' => [],
// Define specific types for model properties
'props_with_types' => [],
// Add custom properties to models
'custom_props' => [
'Organization' => [
'uuid' => 'string',
],
],
];
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
protected $fillable = ['name', 'email', 'bio'];
protected $casts = [
'email_verified_at' => 'datetime',
];
public function posts(): HasMany
{
return $this->hasMany(Post::class);
}
}
// Your model
class Page extends Model
{
protected $casts = [
'metadata' => MetadataCast::class,
];
}
// Your Cast
class MetadataCast implements CastsAttributes
{
public function get($model, string $key, $value, array $attributes): Metadata
{
return Metadata::fromArray(json_decode($value, true));
}
}
// Generated TypeScript
export interface IMetadata {
title: string;
description?: string;
}
export interface IPage {
id: number;
metadata?: IMetadata | null;
}
'data_namespaces' => [
'App\\Data',
],
// App\Data\Knowledge\KnowledgeData
class KnowledgeData extends \Spatie\LaravelData\Data
{
public function __construct(
public readonly string $id,
public readonly KnowledgeState $state, // backed enum
public readonly ?KnowledgeSourceData $source, // nested DTO
/** @var KnowledgeTagData[]|null */
public readonly ?array $tags,
public readonly string|Optional $slug, // may be absent from the payload
public readonly bool $isActive = true,
) {}
public static function fromModel(Knowledge $knowledge): self { /* ... */ }
}