Skip to content

Commit ed205b1

Browse files
authored
Update README.md
1 parent 4bb6298 commit ed205b1

File tree

1 file changed

+46
-10
lines changed

1 file changed

+46
-10
lines changed

README.md

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,62 @@
1-
# typescriptify-laravel-models
1+
# TypeScriptify Laravel Models
22

3-
Effortlessly generate TypeScript interface definitions from Laravel models.
3+
Effortlessly generate TypeScript interface definitions from Laravel Model classes.
44

5-
## Example Usage
5+
## Usage
66

7-
```php
8-
<?php
7+
The main invocation of `php artisan typescriptify:model` with --help lists sub-commands available:
98

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
1120
12-
echo TypeScriptifyModel::generate(\App\Models\User::class);
21+
Options:
22+
--includeHidden Include the protected $hidden properties
1323
```
14-
Will produce:
24+
25+
## Example Usage
26+
Invocating `php artisan typescriptify:model \App\Models\User` on a fresh Laravel installation will produce:
27+
1528
```ts
1629
interface User {
1730
id: number;
1831
name: string;
1932
email: string;
2033
email_verified_at: string|null;
21-
password: string;
22-
remember_token: string|null;
2334
created_at: string|null;
2435
updated_at: string|null;
2536
}
2637
```
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

Comments
 (0)