You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Blueprint will automatically register itself using [package discovery](https://laravel.com/docs/packages#package-discovery).
29
24
30
-
## Requirements
31
-
Blueprint requires a Laravel application running version 6.0 or higher.
32
-
33
-
While Blueprint may be more flexible in a future version, it currently assumes a standard project structure using the default `App` namespace.
34
-
35
25
## Basic Usage
36
-
Blueprint adds multiple artisan commands. The most commonly used command is the `blueprint:build` command to generate the Laravel components:
26
+
Blueprint comes with a set of artisan commands. The one you'll use the most is the `blueprint:build` command to generate the Laravel components:
37
27
38
28
```sh
39
29
php artisan blueprint:build [draft]
40
30
```
41
31
42
-
The _draft_ file contains a [definition of the components](#defining-components) to generate. By default, the `blueprint:build` command attempts to load a `draft.yaml` file from the project root folder.
43
-
44
-
45
-
## Defining Components
46
-
Use Blueprint's `artisan` commands you may generate multiple Laravel components from a single definition called a _draft_ file.
32
+
The _draft_ file contains a [definition of the components](https://blueprint.laravelshift.com/docs/generating-components/) to generate.
47
33
48
-
Within this draft file you define _models_ and _controllers_ using an expressive, human-readable YAML syntax.
49
-
50
-
Let's review the following draft file:
34
+
Let's review the following, example draft file to generate some _blog_ components:
51
35
52
36
```yaml
53
37
models:
@@ -77,267 +61,13 @@ From these simple 20 lines of YAML, Blueprint will generate all of the following
77
61
- A _model_ class for `Post` complete with `fillable`, `casts`, and `dates` properties, as well as relationships methods.
78
62
- A _migration_ to create the `posts` table.
79
63
- A [_factory_](https://laravel.com/docs/database-testing) intelligently setting columns with fake data.
80
-
- A _controller_ class for `PostController` with `index` and `store` actions complete with code generated for each [statement](#statements).
64
+
- A _controller_ class for `PostController` with `index` and `store` actions complete with code generated for each [statement](https://blueprint.laravelshift.com/docs/controller-statements/).
81
65
- _Routes_ for the `PostController` actions.
82
66
- A [_form request_](https://laravel.com/docs/validation#form-request-validation) of `StorePostRequest` validating `title` and `content` based on the `Post` model definition.
83
67
- A _mailable_ class for `ReviewNotification` complete with a `post` property set through the _constructor_.
84
68
- A _job_ class for `SyncMedia` complete with a `post` property set through the _constructor_.
85
69
- An _event_ class for `NewPost` complete with a `post` property set through the _constructor_.
86
70
- A _Blade template_ of `post/index.blade.php` rendered by `PostController@index`.
87
71
88
-
While this draft file only defines a single model and controller, you may define multiple [models](#models) and [controllers](#controllers).
89
-
90
-
### Models
91
-
Within the `models` section of a draft file you may define multiple models. Each model begins with a _name_ followed by a list of columns. Columns are `key: value` pairs where `key` is the column name and `value` defines its attributes.
92
-
93
-
Expanding on the example above, this draft file defines multiple models:
94
-
95
-
```yaml
96
-
models:
97
-
Post:
98
-
title: string:400
99
-
content: longtext
100
-
published_at: nullable timestamp
101
-
102
-
Comment:
103
-
content: longtext
104
-
published_at: nullable timestamp
105
-
106
-
# additional models...
107
-
```
108
-
109
-
From this definition, Blueprint creates two models: `Post`and `Comment`, respectively. You may continue to define additional models.
110
-
111
-
Blueprint recommends defining the model name in its _StudlyCase_, singular form to follow Laravel naming conventions. For example, `Post` instead of `post` or `posts`.
112
-
113
-
Similarly, column names will be used as-is. The attributes of these columns may be any of the [column types](https://laravel.com/docs/migrations#creating-columns) and [column modifiers](https://laravel.com/docs/migrations#column-modifiers) available in Laravel. You may define these as-is or using lowercase.
114
-
115
-
For complex attributes, you may use a `key:value` pair. From these example above, `string:400` defines a `string` column type with a maximum length of `400` characters. Other examples include `enum:'foo','bar','baz'` or `decimal:10,2`.
116
-
117
-
By default, each model will automatically be defined with an `id` and _timestamps_ columns. To disable these columns you may define them with a `false` value. For example, `timestamps: false`.
118
-
119
-
Blueprint also offers additional _shorthands_ which will be expanded into valid YAML. Shorthands include an `id` data type, as well as defining [soft deleting](https://laravel.com/docs/eloquent#soft-deleting) models.
120
-
121
-
For example:
122
-
123
-
```yaml
124
-
models:
125
-
Comment:
126
-
user_id: id
127
-
softDeletes
128
-
# ...
129
-
```
130
-
131
-
Using these shorthands, Blueprint will generate a `Comment` class using the `SoftDeletes` trait. It will also create a `user_id` column with the appropriate data type for an integer foreign key.
132
-
133
-
Blueprint also inspects columns and assigns them to `fillable`, `casts`, and `dates` properties, as well as generate relationships methods.
134
-
135
-
By default, all columns except for `id` and _timestamps_ will be added to the `fillable` property.
136
-
137
-
Where appropriate, Blueprint will [cast](https://laravel.com/docs/5.8/eloquent-mutators#attribute-casting) columns to `integer`, `boolean`, and `decimal` types. Any _date_ columns will be added to the `dates` properties.
138
-
139
-
Columns which use an `id` data type or end with `_id` will be used to generate `belongsTo` relationships. By default, Blueprint uses the column name prefix for the related model. If you define a relationship for a different model, you may use a `id:model` syntax.
140
-
141
-
For example:
142
-
143
-
```yaml
144
-
models:
145
-
Post:
146
-
author_id: id:user
147
-
# ...
148
-
```
149
-
150
-
151
-
### Controllers
152
-
Similar to `models`, you may also define multiple `controllers`. Within the `controllers` section you define a _controller_ by name. Each controller may define multiple `actions` which contain a list of [statements](#statements).
153
-
154
-
Expanding on the example above, this draft file defines multiple controllers:
155
-
156
-
```yaml
157
-
controllers:
158
-
Post:
159
-
index:
160
-
query: all
161
-
render: post.index with:posts
162
-
create:
163
-
render: post.create
164
-
store:
165
-
validate: title, content
166
-
save: post
167
-
redirect: post.index
168
-
169
-
Comment:
170
-
show:
171
-
render: comment.show with:show
172
-
173
-
# additional controller...
174
-
```
175
-
176
-
From this definition, Blueprint will generate two controllers. A `PostController` with `index`, `create`, and `store` actions. And a `CommentController` with a `show` action.
177
-
178
-
While you may specify the full name of a controller, Blueprint will automatically suffix names with `Controller`.
179
-
180
-
Blueprint encourages you to define [resource controllers](https://laravel.com/docs/controllers#resource-controllers). Doing so allows Blueprint to infer details and generate even more code automatically.
181
-
182
-
183
-
#### Statements
184
-
Blueprint comes with an expressive set of statements which implicitly define additional components to generate. Each statement is a `key: value` pair.
185
-
186
-
The `key` defines the _type_ of statement to generate. Currently, Blueprint supports the following types of statements:
187
-
188
-
<dl>
189
-
<dt>validate</dt>
190
-
<dd>
191
-
192
-
Generates a form request with _rules_ based on the referenced model definition. Blueprint accepts a `value` containing a comma separated list of column names.
193
-
194
-
For example:
195
-
196
-
```yaml
197
-
validate: title, content, author_id
198
-
```
199
-
200
-
Blueprint also updates the type-hint of the injected request object.</dd>
201
-
202
-
<dt>find</dt>
203
-
<dd>
204
-
205
-
Generates an Eloquent `find` statement. If the `value` provided is a qualified [reference](#references), Blueprint will expand the reference to determine the model. Otherwise, Blueprint will attempt to use the controller to determine the related model.</dd>
206
-
207
-
<dt>query</dt>
208
-
<dd>
209
-
210
-
Generates an Eloquent query statement using `key:value` pairs provided in `value`. Keys may be any of the basic query builder methods for [`where` clauses](https://laravel.com/docs/queries#where-clauses) and [ordering](https://laravel.com/docs/queries#ordering-grouping-limit-and-offset).
Currently, Blueprint supports generating query statements for `all`, `get`, `pluck`, and `count`.</dd>
219
-
220
-
221
-
<dt>save/delete</dt>
222
-
<dd>
223
-
224
-
Generates an Eloquent statement for saving a model. Blueprint uses the controller action to infer which statement to generate.
225
-
226
-
For example, for a `store` controller action, Blueprint will generate a `Model::create()` statement. Otherwise, a `$model->save()` statement will be generated.
227
-
228
-
Similarly, within a `destroy` controller action, Blueprint will generate a `$model->delete()` statement. Otherwise, a `Model::destroy()` statement will be generated.</dd>
229
-
230
-
<dt>flash</dt>
231
-
<dd>
232
-
233
-
Generates a statement to [flash data](https://laravel.com/docs/session#flash-data) to the session. Blueprint will use the `value` as the session key and expands the reference as the session value.
234
-
235
-
For example:
236
-
237
-
```yaml
238
-
flash: post.title
239
-
```
240
-
</dd>
241
-
242
-
<dt>render</dt>
243
-
<dd>Generates a `return view();` statement complete with a template reference and data.
244
-
245
-
For example:
246
-
247
-
```yaml
248
-
view: post.show with:post
249
-
```
250
-
251
-
When the template does not exist, Blueprint will generate the Blade template for the view.</dd>
252
-
253
-
254
-
<dt>redirect</dt>
255
-
<dd>Generates a `return redirect()` statement using the `value` as a reference to a named route passing any data as parameters.
256
-
257
-
For example:
258
-
259
-
```yaml
260
-
redirect: post.show with:post
261
-
```
262
-
</dd>
263
-
264
-
265
-
<dt>dispatch</dt>
266
-
<dd>
267
-
268
-
Generates a statement to dispatch a [Job](https://laravel.com/docs/queues#creating-jobs) using the `value` to instantiate an object and pass any data.
269
-
270
-
For example:
271
-
272
-
```yaml
273
-
dispatch: SyncMedia with:post
274
-
```
275
-
276
-
If the referenced _job_ class does not exist, Blueprint will create one using any data to define properties and a `__construct` method which assigns them.</dd>
277
-
278
-
<dt>fire</dt>
279
-
<dd>
280
-
281
-
Generates a statement to dispatch a [Event](https://laravel.com/docs/events#defining-events) using the `value` to instantiate the object and pass any data.
282
-
283
-
For example:
284
-
285
-
```yaml
286
-
fire: NewPost with:post
287
-
```
288
-
289
-
If the referenced _event_ class does not exist, Blueprint will create one using any data to define properties and a `__construct` method which assigns them.</dd>
290
-
291
-
292
-
<dt>send</dt>
293
-
<dd>
294
-
295
-
Generates a statement to send a [Mailable](https://laravel.com/docs/mail#generating-mailables) using the `value` to instantiate the object, specify the recipient, and pass any data.
296
-
297
-
For example:
298
-
299
-
```yaml
300
-
send: ReviewNotification to:post.author with:post
301
-
```
302
-
303
-
If the referenced _mailable_ class does not exist, Blueprint will create one using any data to define properties and a `__construct` method which assigns them.</dd>
304
-
</dl>
305
-
306
-
#### References
307
-
For convenience, Blueprint will use the name of a controller to infer the related model. For example, Blueprint will assume a `PostController` relates to a `Post` model.
308
-
309
-
Blueprint also supports a dot (`.`) syntax for more complex references. This allows you to define values which reference columns on other models.
310
-
311
-
For example, to _find_ a `User` model within the `PostController` you may use:
312
-
313
-
```yaml
314
-
controllers:
315
-
Post:
316
-
show:
317
-
find: user.id
318
-
# ...
319
-
```
320
-
321
-
While these references will often be used within _Eloquent_ and `query` statements, they may be used in other statements as well. When necessary, Blueprint will convert these into variable references using an arrow (`->`) syntax.
322
-
323
-
## Additional Console Commands
324
-
Beyond the `blueprint:build` command, Blueprint provides additional commands:
325
-
326
-
<dl>
327
-
<dt>blueprint:erase</dt>
328
-
<dd>
329
-
330
-
Erases the components created by the last _build_ and warns about any updated components.
331
-
332
-
While this command is helpful, it's better to run the `blueprint:build` command from a clean working state and use Git commands like `reset` and `clean` to _undo_ the last _build_.</dd>
333
-
<dt>blueprint:trace</dt>
334
-
<dd>
335
-
336
-
Loads definitions for existing models into the Blueprint cache file (`.blueprint`) so you may reference them in your _draft_ file.</dd>
337
-
</dl>
338
-
339
-
340
-
## Contributing
341
-
Contributions may be made by submitting a Pull Request against the `master` branch. Any submissions should be complete with tests and adhere to the [PSR-2 code style](https://www.php-fig.org/psr/psr-2/).
342
-
343
-
You may also contribute by [opening an issue](https://github.com/laravel-shift/blueprint/issues) to report a bug or suggest a new feature.
72
+
## Documentation
73
+
Browse the [Blueprint Docs](https://blueprint.laravelshift.com/) for full details on [defining models](https://blueprint.laravelshift.com/docs/defining-models/), [defining controllers](https://blueprint.laravelshift.com/docs/defining-controllers/), [advanced configuration](https://blueprint.laravelshift.com/docs/advanced-configuration/), and [extending Blueprint](https://blueprint.laravelshift.com/docs/extending-blueprint/).
0 commit comments