Skip to content

Commit 0678015

Browse files
committed
readme
1 parent e6b0bab commit 0678015

File tree

1 file changed

+52
-5
lines changed

1 file changed

+52
-5
lines changed

README.md

+52-5
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,46 @@
55
[![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)
66
[![Total Downloads](https://img.shields.io/packagist/dt/lepikhinb/laravel-typescript.svg?style=flat-square)](https://packagist.org/packages/lepikhinb/laravel-typescript)
77

8-
---
9-
xxx
10-
---
8+
The package lets you generate TypeScript interfaces from your Laravel models.
119

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+
```
1444

1545
## Installation
1646

47+
**Laravel 8 and PHP 8 are required.**
1748
You can install the package via composer:
1849

1950
```bash
@@ -35,17 +66,33 @@ return [
3566

3667
'output' => resource_path('js/models.d.ts'),
3768

69+
// load namespaces from composer's `dev-autoload`
3870
'autoloadDev' => false,
3971
];
4072

4173
```
4274

4375
## Usage
4476

77+
Generate TypeScript interfaces.
4578
```bash
4679
php artisan typescript:generate
4780
```
4881

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+
4996
## Testing
5097
5198
```bash

0 commit comments

Comments
 (0)