5
5
[ ![ GitHub Code Style Action Status] ( https://img.shields.io/github/workflow/status/lepikhinb/laravel-typescript/Check%20&%20fix%20styling?label=code%20style )] ( https://github.com/lepikhinb/laravel-typescript/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain )
6
6
[ ![ Total Downloads] ( https://img.shields.io/packagist/dt/lepikhinb/laravel-typescript.svg?style=flat-square )] ( https://packagist.org/packages/lepikhinb/laravel-typescript )
7
7
8
- ---
9
- xxx
10
- ---
8
+ The package lets you generate TypeScript interfaces from your Laravel models.
11
9
12
- yyy
13
- zzz
10
+ ## Introduction
11
+ Say you have a model which has several properties (database columns) and multiple relations.
12
+ ``` php
13
+ class Product extends Model
14
+ {
15
+ public function category(): BelongsTo
16
+ {
17
+ return $this->belongsTo(Category::class);
18
+ }
19
+
20
+ public function features(): HasMany
21
+ {
22
+ return $this->hasMany(Feature::class);
23
+ }
24
+ }
25
+ ```
26
+
27
+ Laravel TypeScript will generate the following namespaced TypeScript interface:
28
+
29
+ ``` typescript
30
+ declare namespace App .Models {
31
+ export interface Product {
32
+ id: number ;
33
+ category_id: number ;
34
+ name: string ;
35
+ price: number ;
36
+ created_at: string | null ;
37
+ updated_at: string | null ;
38
+ category? : App .Models .Category | null ;
39
+ features? : Array <App .Models .Feature > | null ;
40
+ }
41
+ ...
42
+ }
43
+ ```
14
44
15
45
## Installation
16
46
47
+ ** Laravel 8 and PHP 8 are required.**
17
48
You can install the package via composer:
18
49
19
50
``` bash
@@ -35,17 +66,33 @@ return [
35
66
36
67
'output' => resource_path('js/models.d.ts'),
37
68
69
+ // load namespaces from composer's `dev-autoload`
38
70
'autoloadDev' => false,
39
71
];
40
72
41
73
```
42
74
43
75
## Usage
44
76
77
+ Generate TypeScript interfaces.
45
78
``` bash
46
79
php artisan typescript:generate
47
80
```
48
81
82
+ Example usage with Vue 3:
83
+ ``` typescript
84
+ import { defineComponent , PropType } from " vue" ;
85
+
86
+ export default defineComponent ({
87
+ props: {
88
+ product: {
89
+ type: Object as PropType <App .Models .Product >,
90
+ required: true ,
91
+ },
92
+ },
93
+ }
94
+ ` ` `
95
+
49
96
## Testing
50
97
51
98
` ` ` bash
0 commit comments