Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Model Typescript Transfomer Laravel Package

nolanos/laravel-model-typescript-transfomer

View on GitHub
Deep Wiki
Context7

Laravel Model Typescript Transformer

This package generates TypeScript definitions for Laravel Eloquent models using spatie/laravel-typescript-transformer.

Installation

Install via Composer:

composer require nolanos/laravel-model-typescript-transformer

Update your config/typescript-transformer.php config file by...

  • Appending ModelTypeScriptCollector to the collectors array.
  • Appending ModelTransformer to the transformers array.
    'collectors' => [
        // ...
        Nolanos\LaravelModelTypescriptTransformer\ModelTypeScriptCollector::class,
    ],
    'transformers' => [
        // Etc....
        Nolanos\LaravelModelTypescriptTransformer\ModelTransformer::class,
    ],

Usage

Generate TypeScript definitions by running:

php artisan typescript:generate

How it works

Identifying Properties

The ModelTransformer identifies the properties of the given Model by looking at the columns in the database table, and then filters columns out based on the $hidden and $visible properties of the model.

Mapping Database Types to TypeScript Types

Property types are determined by mapping the database column type to a TypeScript type. The following mappings are used:

Typescript Type Database Type
string uuid, string, text, varchar, character varying, date, datetime, timestamp, timestamp without time zone, bpchar, timestamptz, time, bytea, blob
number integer, bigint, int2, int4, int8, float, double, decimal, float8, numeric
boolean boolean, bool

Note: Unknown column types will be mapped to unknown and are followed by a comment stating the db type.

Nullable Types

If a column is nullable, the TypeScript type will be suffixed with | null.

Example

Let's look at a simple of example of a user model in a PostgreSQL database.

CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    email VARCHAR(255) NOT NULL ,
    password VARCHAR(255),
    active BOOLEAN NOT NULL,
    created_at TIMESTAMP,
    updated_at TIMESTAMP
);

The User model might look like this:

class User extends Model
{
    protected $hidden = ['password'];
}

Will generate the following TypeScript definition:

declare namespace App.Models {
  export interface User {
    id: number
    name: string
    email: string
    active: boolean
    created_at: string | null
    updated_at: string | null
  }
}

Limitations

This package has some limitations.

Take a look at the issues to see what's missing.

Development

Setup

git clone git@github.com:nolanos/laravel-model-typescript-transfomer.git

cd laravel-model-typescript-transformer

composer install

Running Tests


composer test

Publishing new Versions

To publish a new version of the package, you need to create a new tag and push it to the repository.

git tag vx.x.x
git push origin vx.x.x

Go to Packagist and click on "Update" to update the package.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge