@@ -29,17 +29,17 @@ need to add our other three models.
29
29
To do this, we will run the following commands:
30
30
31
31
``` bash
32
- vendor/bin/sail artisan make:model Post --migration
33
- vendor/bin/sail artisan make:model Tag --migration
34
- vendor/bin/sail artisan make:model Comment --migration
32
+ herd php artisan make:model Post --migration
33
+ herd php artisan make:model Tag --migration
34
+ herd php artisan make:model Comment --migration
35
35
```
36
36
37
37
You'll see output like the following:
38
38
39
39
```
40
- INFO Model [app/Models/Post.php] created successfully.
40
+ INFO Model [app/Models/Post.php] created successfully.
41
41
42
- INFO Migration [database/migrations/2023_05_15_023525_create_posts_table .php] created successfully.
42
+ INFO Migration [database/migrations/2024_09_30_171953_create_posts_table .php] created successfully.
43
43
```
44
44
45
45
These generator commands created a number of files; let's take a look at a few
@@ -56,10 +56,8 @@ return new class extends Migration
56
56
{
57
57
/**
58
58
* Run the migrations.
59
- *
60
- * @return void
61
59
*/
62
- public function up()
60
+ public function up(): void
63
61
{
64
62
Schema::create('posts', function (Blueprint $table) {
65
63
$table->id();
@@ -69,14 +67,12 @@ return new class extends Migration
69
67
70
68
/**
71
69
* Reverse the migrations.
72
- *
73
- * @return void
74
70
*/
75
- public function down()
71
+ public function down(): void
76
72
{
77
73
Schema::dropIfExists('posts');
78
74
}
79
- }
75
+ };
80
76
```
81
77
82
78
This file contains a * migration* , a class that tells Laravel how to make a change
@@ -117,10 +113,8 @@ In the `_create_tags_table` migration, we will add the following:
117
113
{
118
114
/**
119
115
* Run the migrations.
120
- *
121
- * @return void
122
116
*/
123
- public function up()
117
+ public function up(): void
124
118
{
125
119
Schema::create('tags', function (Blueprint $table) {
126
120
$table->id();
@@ -137,15 +131,13 @@ In the `_create_tags_table` migration, we will add the following:
137
131
138
132
/**
139
133
* Reverse the migrations.
140
- *
141
- * @return void
142
134
*/
143
- public function down()
135
+ public function down(): void
144
136
{
145
137
+ Schema::dropIfExists('post_tag');
146
138
Schema::dropIfExists('tags');
147
139
}
148
- }
140
+ };
149
141
```
150
142
151
143
Notice we've added the ` name ` column to the ` tags ` table. Then we also create
@@ -174,26 +166,18 @@ Now that we've updated all the migrations, we can run them to create our
174
166
database tables:
175
167
176
168
``` bash
177
- vendor/bin/sail artisan migrate
169
+ herd php artisan migrate
178
170
```
179
171
180
172
You should see output like the following, which includes a few standard
181
173
Laravel migrations and the migrations we have added:
182
174
183
175
```
184
- INFO Preparing database.
185
-
186
- Creating migration table .............................. 54ms DONE
187
-
188
- INFO Running migrations.
176
+ INFO Running migrations.
189
177
190
- 2014_10_12_000000_create_users_table .................. 80ms DONE
191
- 2014_10_12_100000_create_password_reset_tokens_table .. 159ms DONE
192
- 2019_08_19_000000_create_failed_jobs_table ............ 142ms DONE
193
- 2019_12_14_000001_create_personal_access_tokens_table . 239ms DONE
194
- 2023_05_15_023525_create_posts_table .................. 279ms DONE
195
- 2023_05_15_023535_create_tags_table ................... 586ms DONE
196
- 2023_05_15_023543_create_comments_table ............... 372ms DONE
178
+ 2024_09_30_171953_create_posts_table ......................... 2.00ms DONE
179
+ 2024_09_30_171959_create_tags_table .......................... 1.73ms DONE
180
+ 2024_09_30_172006_create_comments_table ...................... 0.35ms DONE
197
181
```
198
182
199
183
Now we can look at the model classes. Take a look at the ` /app/Models/Post.php `
@@ -352,7 +336,7 @@ populated into the database.
352
336
Generate a seeder file using the following command:
353
337
354
338
``` bash
355
- vendor/bin/sail artisan make:seeder PostSeeder
339
+ herd php artisan make:seeder PostSeeder
356
340
```
357
341
358
342
This will create a file ` PostSeeder.php ` in the ` /database/seeders ` folder.
@@ -366,16 +350,15 @@ seeder so it looks like this:
366
350
+ use App\Models\Post;
367
351
+ use App\Models\Tag;
368
352
+ use App\Models\User;
353
+ - use Illuminate\Database\Console\Seeds\WithoutModelEvents;
369
354
use Illuminate\Database\Seeder;
370
355
371
356
class PostSeeder extends Seeder
372
357
{
373
358
/**
374
359
* Run the database seeds.
375
- *
376
- * @return void
377
360
*/
378
- public function run()
361
+ public function run(): void
379
362
{
380
363
- //
381
364
+ $author = User::create([
@@ -438,12 +421,15 @@ in the same directory:
438
421
{
439
422
/**
440
423
* Seed the application's database.
441
- *
442
- * @return void
443
424
*/
444
- public function run()
425
+ public function run(): void
445
426
{
446
- - // \App\Models\User::factory(10)->create();
427
+ - // User::factory(10)->create();
428
+ -
429
+ - User::factory()->create([
430
+ - 'name' => 'Test User',
431
+
432
+ - ]);
447
433
+ $this->call(PostSeeder::class);
448
434
}
449
435
}
@@ -452,7 +438,7 @@ in the same directory:
452
438
Then all we need to do is run the seed command to populate the database:
453
439
454
440
``` bash
455
- vendor/bin/sail artisan db:seed
441
+ herd php artisan db:seed
456
442
```
457
443
458
444
## In Summary
0 commit comments