PHP package for Laravel to type Eloquent models, routes, Spatie Settings with autogenerated TypeScript.
If you want to use some helpers with Inertia, you can install associated NPM package.
Note
kiwilan/typescriptable-laravel
: current PHP package for Laravel.@kiwilan/typescriptable-laravel
: optional NPM package to use with Vite and Inertia to have some helpers, if you want to know more about, check documentation.tightenco/ziggy
is required only for@kiwilan/typescriptable-laravel
(NPM package)
- π½ All Laravel databases are supported: MySQL, PostgreSQL, SQLite, SQL Server
- π¬ Generate TS types for Eloquent models
- π Generate TS types for Eloquent relations
- πͺ Generate TS types for
casts
(include nativeenum
support) - π
Generate TS types for
dates
- π Generate TS types for
appends
withaccessors
- Partial for
Illuminate\Database\Eloquent\Casts\Attribute
- Old way
get*Attribute
methods are totally supported
- Partial for
- #οΈβ£ Generate TS types for
counts
- π Can generate pagination TS types for Laravel pagination
- πΎ Can generate simple PHP classes from Eloquent models
- βοΈ Generate TS types for
spatie/laravel-settings
- π£ Generate TS types for Laravel routes
- Scan route parameters
- For Inertia, you can install
@kiwilan/typescriptable-laravel
NPM package to use some helpers
- β
Multiple commands to generate types
php artisan typescriptable
for models, settings and routes (safe even if you don't use all)php artisan typescriptable:models
for Eloquent modelsphp artisan typescriptable:settings
forspatie/laravel-settings
php artisan typescriptable:routes
for Laravel routes
- Improve
Casts\Attribute
methods - Add parser for calebporzio/sushi
- Add parser for spatie/laravel-permission
- Add parser for mongodb
This version requires PHP 8.2+
and supports Laravel 11.
Warning
Laravel 11 dropped Doctrine DBAL. For previous Laravel versions, you can use 1.12.03
version.
Version | L9 | L10 | L11 |
---|---|---|---|
1.12.03 | β | β | β |
2.0.0 | β | β | β |
You can install the package via composer:
With Laravel v11+ and PHP 8.2
composer require kiwilan/typescriptable-laravel
With Laravel v9-10 and PHP 8.1
composer require kiwilan/typescriptable-laravel:1.12.03
If you want to use .d.ts
files, you need to use TypeScript in your Laravel project, you have to create a tsconfig.json
file and add .d.ts
paths in compilerOptions.types
:
Note
If you change paths into config or with options, adapt paths.
{
"compilerOptions": {
"typeRoots": ["./node_modules/@types", "resources/**/*.d.ts"]
},
"include": ["resources/**/*.d.ts"]
}
You can publish the config file
php artisan vendor:publish --tag="typescriptable-config"
php artisan typescriptable
With options:
- --
M
|models
: Generate Models types. - --
R
|routes
: Generate Routes types. - --
S
|settings
: Generate Settings types.
Generate resources/js/types-models.d.ts
file with all models types.
php artisan typescriptable:models
With options:
- --
M
|models-path
: Path to models directory. - --
O
|output-path
: Path to output. - --
P
|php-path
: Path to output PHP classes, if null will not print PHP classes.
If you use spatie/laravel-settings
, you can generate resources/js/types-settings.d.ts
file with all settings types.
php artisan typescriptable:settings
With options:
- --
S
|settings-path
: Path to settings directory. - --
O
|output-path
: Path to output.
Generate resources/js/types-routes.d.ts
file with all routes types and resources/js/routes.ts
for routes references.
php artisan typescriptable:routes
With options:
- --
R
|routes-path
: Path to routes directory. - --
O
|output-path
: Path to output.
If you have a database prefix, you can add it in config/typescriptable.php
file with DB_PREFIX
env variable.
return [
'database_prefix' => env('DB_PREFIX', ''),
];
Or you can use DB_PREFIX
into config/database.php
file.
'prefix' => env('DB_PREFIX', ''),
Two configs works.
kiwilan/typescriptable-laravel
will cover many cases, but if you want to override some models, you can just create a type like resources/js/types/index.ts
and extends Model
type.
interface BookAdvanced extends App.Models.Book {
pivot: {
created_at: string;
updated_at: string;
};
}
And you can import custom type in your code when you need to use advanced type.
Check examples documentation.
Create a .env
file with your database configuration
cp .env.example .env
And you can run tests
composer test
Note
To install this on M1 Mac, you need to enable Use Rosetta for x86/amd64 emulation on Apple Silicon
in Docker preferences.
To install MySQL with Docker
docker run --name mysql \
-e MYSQL_ROOT_PASSWORD=root \
-e MYSQL_USER=testing \
-e MYSQL_PASSWORD=testing \
-e MYSQL_DATABASE=testing \
-p 3306:3306 \
-d \
mysql:8.0
To install PostgreSQL with Docker
docker run --name postgresql \
-e POSTGRES_USER=testing \
-e POSTGRES_PASSWORD=testing \
-e POSTGRES_DB=testing \
-p 5432:5432 \
-d \
postgres:15.4
To install SQL Server with Docker
Warning
If you have an error like this: "An invalid attribute was designated on the PDO object", you have to update msphpsql
driver. Check laravel/framework#47937 for more information.
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=12345OHdf%e" \
-p 1433:1433 \
--name sqlserver \
--hostname sqlserver \
-d \
mcr.microsoft.com/mssql/server:2022-latest
Please see CHANGELOG for more information on what has changed recently.
- Spatie: for
spatie/package-skeleton-laravel
- Ewilan Riviere: Author package
The MIT License (MIT). Please see License File for more information.