|
4 | 4 |
|
5 | 5 | Laravel-Entity-Generator - This generator is used to create a standard class stack for a new entity. |
6 | 6 |
|
7 | | -### Install |
| 7 | +## Installation |
8 | 8 |
|
9 | | -We're highly recommending to install package for only dev environment |
| 9 | +Install package using `dev` mode |
10 | 10 |
|
11 | 11 | ```bash |
12 | | - composer require ronasit/laravel-entity-generator --dev |
| 12 | +composer require ronasit/laravel-entity-generator --dev |
13 | 13 | ``` |
14 | 14 |
|
15 | | -If you're on Laravel 5.5 or later the package will be auto-discovered. |
16 | | -Otherwise, you will need to manually configure it in your config/app.php. |
| 15 | +Publish package's resources. |
17 | 16 |
|
18 | | -```php |
19 | | -'providers' => [ |
20 | | - // ... |
21 | | - RonasIT\Support\EntityGeneratorServiceProvider::class, |
22 | | -], |
| 17 | +```bash |
| 18 | +php artisan vendor:publish --provider="RonasIT\Support\EntityGeneratorServiceProvider" |
23 | 19 | ``` |
24 | 20 |
|
25 | | -For dev installation provider should be registered optionally |
26 | | - |
27 | | -```php |
28 | | -class AppServiceProvider |
29 | | -{ |
30 | | - public function boot(): void |
31 | | - { |
32 | | - // ... |
33 | | - if (config('app.env') === 'local') { |
34 | | - App::register(\RonasIT\Support\EntityGeneratorServiceProvider::class); |
35 | | - } |
36 | | - } |
37 | | -} |
38 | | -``` |
| 21 | +## Usage |
39 | 22 |
|
40 | | -And publish. |
| 23 | +### Entity generation |
41 | 24 |
|
42 | | -```bash |
43 | | - php artisan vendor:publish --provider="RonasIT\Support\EntityGeneratorServiceProvider" |
44 | | -``` |
| 25 | +Call `make:entity` artisan command to run the generation command for the full stack of the entity classes: |
45 | 26 |
|
46 | | -### Examples |
47 | 27 | ```bash |
48 | | - php artisan make:entity EntityName \ |
49 | | - -S required_string_field \ |
50 | | - --integer=not_required_integer_field \ |
51 | | - --boolean-required=required_boolean_field \ |
52 | | - -j data \ |
53 | | - -e AnotherEntityName |
| 28 | +php artisan make:entity Post |
54 | 29 | ``` |
55 | 30 |
|
56 | | -### Documentation |
57 | | - |
58 | | -`make:entity` artisan command - generate stack of classes to work with the new entity in project. |
59 | | - |
60 | | -Syntax: |
| 31 | +Entity name may contain the subfolder, in this case generated `model` and `nova resource` will be placed to |
| 32 | +the subfolder as well: |
61 | 33 |
|
62 | 34 | ```bash |
63 | | -> php artisan make:entity [entity-name] [options] |
| 35 | +php artisan make:entity Forum/Blog/Post |
64 | 36 | ``` |
65 | 37 |
|
66 | | -`entity-name` - Name of the Entity, recommended to use `CamelCase` naming style e.g. `WhitelistedDomain` |
67 | | - |
68 | | -`options` - one or more options from the lists below |
69 | | - |
70 | 38 | #### Fields definition options |
71 | 39 |
|
72 | | - -i|--integer : Add integer field to entity. |
73 | | - |
74 | | - -I|--integer-required : Add required integer field to entity. If you want to specify default value you have to do it manually. |
75 | | - |
76 | | - -f|--float : Add float field to entity. |
77 | | - |
78 | | - -F|--float-required : Add required float field to entity. If you want to specify default value you have to do it manually. |
79 | | - |
80 | | - -s|--string : Add string field to entity. Default type is VARCHAR(255) but you can change it manually in migration. |
81 | | - |
82 | | - -S|--string-required : Add required string field to entity. If you want to specify default value ir size you have to do it manually. |
| 40 | +The `make:entity` provides an ability to set the entity's fields, which will be used in all created classes (e.g. Model, Create/Update requests, Test fixtures, etc.) |
83 | 41 |
|
84 | | - -b|--boolean : Add boolean field to entity. |
85 | | - |
86 | | - -B|--boolean-required : Add boolean field to entity. If you want to specify default value you have to do it manually. |
| 42 | +```bash |
| 43 | +php artisan make:entity Post -S title -S text -t published_at |
| 44 | +``` |
87 | 45 |
|
88 | | - -t|--timestamp : Add timestamp field to entity. |
| 46 | +The following field types are available to use: |
| 47 | + |
| 48 | +| Type | Modificator | Short Option | Full Option | |
| 49 | +| -------- | -------- | ------- | ------- | |
| 50 | +| `integer` | | `-i` | `--integer` | |
| 51 | +| `integer` | required | `-I` | `--integer-required` | |
| 52 | +| `float` | | `-f` | `--float` | |
| 53 | +| `float` | required | `-F` | `--float-required` | |
| 54 | +| `string` | | `-s` | `--string` | |
| 55 | +| `string` | required | `-S` | `--string-required` | |
| 56 | +| `boolean` | | `-b` | `--boolean` | |
| 57 | +| `boolean` | required | `-B` | `--boolean-required` | |
| 58 | +| `datetime` | | `-t` | `--timestamp` | |
| 59 | +| `datetime` | required | `-T` | `--timestamp-required` | |
| 60 | +| `json` | | `-j` | `--json` | |
89 | 61 |
|
90 | | - -T|--timestamp-required : Add timestamp field to entity. If you want to specify default value you have to do it manually. |
| 62 | +#### Relations definitions options |
91 | 63 |
|
92 | | - -j|--json : Add json field to entity. |
| 64 | +Command also provides an ability to set relations, which will be added to the model |
93 | 65 |
|
94 | | -#### Relations definitions options |
| 66 | +```bash |
| 67 | +php artisan make:entity Post -A Comment -A Reaction |
| 68 | +``` |
95 | 69 |
|
96 | | - -a|--has-one : Set hasOne relations between you entity and existed entity. |
| 70 | +Relation may be placed in the subfolder, in this case - set the relative namespace from the model directory: |
97 | 71 |
|
98 | | - -A|--has-many : Set hasMany relations between you entity and existed entity. |
| 72 | +```bash |
| 73 | +php artisan make:entity Post -A Blog/Comment -A Forum/Common/Reaction |
| 74 | +``` |
99 | 75 |
|
100 | | - -e|--belongs-to : Set belongsTo relations between you entity and existed entity. |
| 76 | +The following options are available to set relations: |
101 | 77 |
|
102 | | - -E|--belongs-to-many : Set belongsToMany relations between you entity and existed entity. |
| 78 | +| Type | Short Option | Full Option | |
| 79 | +| -------- | ------- | ------- | |
| 80 | +| Has one | `-a` | `--has-one` | |
| 81 | +| Has many | `-A` | `--has-many` | |
| 82 | +| Belongs to | `-e` | `--belongs-to` | |
| 83 | +| Belongs to many | `-E` | `--belongs-to-many` | |
103 | 84 |
|
104 | 85 | #### Single class generation mode options |
105 | 86 |
|
106 | | - --only-model : Set this flag if you want to create only model. This flag is a higher priority than --only-migration, --only-tests and --only-repository. |
107 | | - |
108 | | - --only-repository : Set this flag if you want to create only repository. This flag is a higher priority than --only-tests and --only-migration. |
| 87 | +Command allows to generate only single entity-related class |
109 | 88 |
|
110 | | - --only-service : Set this flag if you want to create only service. |
| 89 | +```bash |
| 90 | +php artisan make:entity Post --only-tests |
| 91 | +``` |
111 | 92 |
|
112 | | - --only-controller : Set this flag if you want to create only controller. |
| 93 | +The following options are available: |
113 | 94 |
|
114 | | - --only-requests : Set this flag if you want to create only requests. |
| 95 | +- `--only-model` |
| 96 | +- `--only-repository` |
| 97 | +- `--only-service` |
| 98 | +- `--only-controller` |
| 99 | +- `--only-requests` |
| 100 | +- `--only-migration` |
| 101 | +- `--only-factory` |
| 102 | +- `--only-tests` |
| 103 | +- `--only-seeder` |
| 104 | +- `--only-resource` |
115 | 105 |
|
116 | | - --only-migration : Set this flag if you want to create only repository. This flag is a higher priority than --only-tests. |
| 106 | +#### Mode combination options |
117 | 107 |
|
118 | | - --only-factory : Set this flag if you want to create only factory. |
| 108 | +Sometimes you need to generate the stack of classes to work with some entity only inside the application without |
| 109 | +the ability to manipulate it using API, or you need to create only API for already existed entity. |
119 | 110 |
|
120 | | - --only-tests : Set this flag if you want to create only tests. |
| 111 | +For this task, there are two mutually exclusive options: |
121 | 112 |
|
122 | | - --only-seeder : Set this flag if you want to create only seeder. |
| 113 | +1. `--only-entity`, generate stack of classes to work with entity only inside the app, it includes: |
| 114 | +- `migration` |
| 115 | +- `model` |
| 116 | +- `service` |
| 117 | +- `repository` |
123 | 118 |
|
124 | | - --only-resource : Set this flag if you want to create only resource. |
| 119 | +2. `--only-api`, generate stack of classes to implement API part to work with already existed entity: |
| 120 | +- `routes` |
| 121 | +- `controller` |
| 122 | +- `requests` |
| 123 | +- `tests` |
125 | 124 |
|
126 | | -#### Mode combination options |
| 125 | +#### Methods setting options |
127 | 126 |
|
128 | | - --only-entity : Generate stack of classes to work with entity inside the app (Migration/Model/Service/Repository) |
| 127 | +By default, the command generate all methods from the CRUD stack which includes: |
| 128 | +- create |
| 129 | +- update |
| 130 | +- delete |
| 131 | +- search |
| 132 | +- get by id |
129 | 133 |
|
130 | | - --only-api : Generate stack of classes to implement API part to work with entity (routes/Controller/Requests/Tests) |
| 134 | +But what if we need to create the interface only for updating and reading the entity? Just use the `methods` option for it: |
131 | 135 |
|
132 | | -#### Additional generation options |
| 136 | +```bash |
| 137 | +php artisan make:entity Post --methods=RU |
| 138 | +``` |
133 | 139 |
|
134 | | - --methods=[default: CRUD] : Don't work for `--only-entity` option. Will generate API classes (routes, controller's |
135 | | - methods, requests, tests) only for choosed methods. |
136 | | - C - created |
137 | | - R - read (search and get by id) |
138 | | - U - update |
139 | | - D - delete |
| 140 | +Feel free to use any combinations of actions in any order. Each action has its own character: |
| 141 | +- `C` create |
| 142 | +- `U` update |
| 143 | +- `D` delete |
| 144 | +- `R` read (search and get by id) |
140 | 145 |
|
141 | 146 | ## Release notes |
142 | 147 |
|
|
0 commit comments