@@ -29,17 +29,17 @@ need to add our other three models.
2929To do this, we will run the following commands:
3030
3131``` 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
3535```
3636
3737You'll see output like the following:
3838
3939```
40- INFO Model [app/Models/Post.php] created successfully.
40+ INFO Model [app/Models/Post.php] created successfully.
4141
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.
4343```
4444
4545These generator commands created a number of files; let's take a look at a few
@@ -56,10 +56,8 @@ return new class extends Migration
5656{
5757 /**
5858 * Run the migrations.
59- *
60- * @return void
6159 */
62- public function up()
60+ public function up(): void
6361 {
6462 Schema::create('posts', function (Blueprint $table) {
6563 $table->id();
@@ -69,14 +67,12 @@ return new class extends Migration
6967
7068 /**
7169 * Reverse the migrations.
72- *
73- * @return void
7470 */
75- public function down()
71+ public function down(): void
7672 {
7773 Schema::dropIfExists('posts');
7874 }
79- }
75+ };
8076```
8177
8278This 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:
117113 {
118114 /**
119115 * Run the migrations.
120- *
121- * @return void
122116 */
123- public function up()
117+ public function up(): void
124118 {
125119 Schema::create('tags', function (Blueprint $table) {
126120 $table->id();
@@ -137,15 +131,13 @@ In the `_create_tags_table` migration, we will add the following:
137131
138132 /**
139133 * Reverse the migrations.
140- *
141- * @return void
142134 */
143- public function down()
135+ public function down(): void
144136 {
145137+ Schema::dropIfExists('post_tag');
146138 Schema::dropIfExists('tags');
147139 }
148- }
140+ };
149141```
150142
151143Notice 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
174166database tables:
175167
176168``` bash
177- vendor/bin/sail artisan migrate
169+ herd php artisan migrate
178170```
179171
180172You should see output like the following, which includes a few standard
181173Laravel migrations and the migrations we have added:
182174
183175```
184- INFO Preparing database.
185-
186- Creating migration table .............................. 54ms DONE
187-
188- INFO Running migrations.
176+ INFO Running migrations.
189177
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
197181```
198182
199183Now we can look at the model classes. Take a look at the ` /app/Models/Post.php `
@@ -352,7 +336,7 @@ populated into the database.
352336Generate a seeder file using the following command:
353337
354338``` bash
355- vendor/bin/sail artisan make:seeder PostSeeder
339+ herd php artisan make:seeder PostSeeder
356340```
357341
358342This will create a file ` PostSeeder.php ` in the ` /database/seeders ` folder.
@@ -366,16 +350,15 @@ seeder so it looks like this:
366350+ use App\Models\Post;
367351+ use App\Models\Tag;
368352+ use App\Models\User;
353+ - use Illuminate\Database\Console\Seeds\WithoutModelEvents;
369354 use Illuminate\Database\Seeder;
370355
371356 class PostSeeder extends Seeder
372357 {
373358 /**
374359 * Run the database seeds.
375- *
376- * @return void
377360 */
378- public function run()
361+ public function run(): void
379362 {
380363- //
381364+ $author = User::create([
@@ -438,12 +421,15 @@ in the same directory:
438421 {
439422 /**
440423 * Seed the application's database.
441- *
442- * @return void
443424 */
444- public function run()
425+ public function run(): void
445426 {
446- - // \App\Models\User::factory(10)->create();
427+ - // User::factory(10)->create();
428+ -
429+ - User::factory()->create([
430+ - 'name' => 'Test User',
431+ 432+ - ]);
447433+ $this->call(PostSeeder::class);
448434 }
449435 }
@@ -452,7 +438,7 @@ in the same directory:
452438Then all we need to do is run the seed command to populate the database:
453439
454440``` bash
455- vendor/bin/sail artisan db:seed
441+ herd php artisan db:seed
456442```
457443
458444## In Summary
0 commit comments