|
1 | | -# typescriptify-laravel-models |
| 1 | +# TypeScriptify Laravel Models |
2 | 2 |
|
3 | | -Effortlessly generate TypeScript interface definitions from Laravel models. |
| 3 | +Effortlessly generate TypeScript interface definitions from Laravel Model classes. |
4 | 4 |
|
5 | | -## Example Usage |
| 5 | +## Usage |
6 | 6 |
|
7 | | -```php |
8 | | -<?php |
| 7 | +The main invocation of `php artisan typescriptify:model` with --help lists sub-commands available: |
9 | 8 |
|
10 | | -use SalemC\TypeScriptifyLaravelModels\TypeScriptifyModel; |
| 9 | +``` |
| 10 | +php artisan typescriptify:model --help |
| 11 | +
|
| 12 | +Description: |
| 13 | + Convert a model to it's TypeScript definition. |
| 14 | +
|
| 15 | +Usage: |
| 16 | + typescriptify:model [options] [--] <model> |
| 17 | +
|
| 18 | +Arguments: |
| 19 | + model The fully qualified class name for the model - e.g. App\Models\User |
11 | 20 |
|
12 | | -echo TypeScriptifyModel::generate(\App\Models\User::class); |
| 21 | +Options: |
| 22 | + --includeHidden Include the protected $hidden properties |
13 | 23 | ``` |
14 | | -Will produce: |
| 24 | + |
| 25 | +## Example Usage |
| 26 | +Invocating `php artisan typescriptify:model \App\Models\User` on a fresh Laravel installation will produce: |
| 27 | + |
15 | 28 | ```ts |
16 | 29 | interface User { |
17 | 30 | id: number; |
18 | 31 | name: string; |
19 | 32 | email: string; |
20 | 33 | email_verified_at: string|null; |
21 | | - password: string; |
22 | | - remember_token: string|null; |
23 | 34 | created_at: string|null; |
24 | 35 | updated_at: string|null; |
25 | 36 | } |
26 | 37 | ``` |
| 38 | + |
| 39 | +Or if you prefer, you can instantiate your own version of the `TypeScriptifyModel` class: |
| 40 | + |
| 41 | +```php |
| 42 | +use SalemC\TypeScriptifyLaravelModels\TypeScriptifyModel; |
| 43 | + |
| 44 | +echo (new TypeScriptifyModel(\App\Models\User::class))->generate(); |
| 45 | +``` |
| 46 | + |
| 47 | +## How It Works |
| 48 | + |
| 49 | +### Database |
| 50 | + |
| 51 | +**TypeScriptifyModels** works primarily by gathering column data from the database your Laravel instance is setup with. Once gathered, it maps column types to known TypeScript types. |
| 52 | + |
| 53 | +### Casts |
| 54 | + |
| 55 | +**TypeScriptifyModels** also respects _predictable_ Laravel casts (specifically `protected $casts` and `protected $dates`) you've setup in the model being converted. It will map all known casts to TypeScript types. |
| 56 | + |
| 57 | +## Caveats |
| 58 | + |
| 59 | +**TypeScriptifyModels** is only able to map _predictable_ data types to TypeScript types. [Custom casts](https://laravel.com/docs/9.x/eloquent-mutators#custom-Casts) and [Custom Accessors](https://laravel.com/docs/9.x/eloquent-mutators#accessors-and-mutators) are not, and cannot be supported. |
| 60 | + |
| 61 | +If **TypeScriptifyModels** fails to map a type to a TypeScript type, it will set the value to `unknown` in the TypeScript interface definition. |
| 62 | + |
0 commit comments