1 00:00:00,890 --> 00:00:02,086 [SOUND] Welcome back. 2 00:00:02,086 --> 00:00:04,309 We're making significant progress. 3 00:00:04,309 --> 00:00:08,151 And now that we've created our models and migrations, 4 00:00:08,151 --> 00:00:11,594 we're ready to dive into factories and seeders. 5 00:00:11,594 --> 00:00:15,334 So why do we need to use factories and seeders? 6 00:00:15,334 --> 00:00:19,002 Imagine you want to add some test data for our API but 7 00:00:19,002 --> 00:00:24,122 you don't want to manually define all of the individual attributes for 8 00:00:24,122 --> 00:00:28,063 each model, which can be a tedious development task. 9 00:00:28,063 --> 00:00:33,103 Instead, we're going to use model factories to generate large amounts of 10 00:00:33,103 --> 00:00:38,310 database records for each model instance using the powerful Faker library. 11 00:00:39,420 --> 00:00:44,630 To learn more about factories and seeders, check out the teachers notes below. 12 00:00:44,630 --> 00:00:45,406 Let's get started. 13 00:00:47,618 --> 00:00:53,391 The factories, migrations and the seeds folders are inside of the app/database 14 00:00:53,391 --> 00:00:58,338 directory, where you'll also find the DatabaseSeeder.php file. 15 00:00:58,338 --> 00:01:02,607 Which we'll populate with to seeders, AuthorsTableSeeder and 16 00:01:02,607 --> 00:01:06,574 BooksTableSeeder, using two artisan commands like this. 17 00:01:14,169 --> 00:01:19,746 To test our connection, use the php artisan migrate:fresh command. 18 00:01:22,931 --> 00:01:27,486 As you can see, we now have two seeders in our seeds directory. 19 00:01:27,486 --> 00:01:31,189 AuthorsTableSeeder and BooksTableSeeder. 20 00:01:31,189 --> 00:01:33,537 We're making great progress, and 21 00:01:33,537 --> 00:01:38,554 we're ready to test our seeders by using the db:seed command, like this. 22 00:01:45,943 --> 00:01:49,579 If everything went well, you should see a message that reads, 23 00:01:49,579 --> 00:01:52,199 database seeding completed successfully. 24 00:01:52,199 --> 00:01:56,330 Also note that they're both empty at the moment, but that's because we 25 00:01:56,330 --> 00:02:00,950 have not specified values for each column, which we will do later in the course. 26 00:02:02,110 --> 00:02:06,983 Finally, let's add both seeders to the DatabaseSeeder.php file inside 27 00:02:06,983 --> 00:02:09,160 of the run function, like this. 28 00:02:15,450 --> 00:02:19,830 Currently, our seeders are empty because we haven't defined them yet. 29 00:02:19,830 --> 00:02:26,177 So let's open the UserFactory.php file, which is a default Laravel factory. 30 00:02:26,177 --> 00:02:30,627 And let's take a look at the defined user factory data inside. 31 00:02:30,627 --> 00:02:35,879 As you can see, the test data has defined the name, email, 32 00:02:35,879 --> 00:02:42,083 email_verified_at, password and remember_token attributes, 33 00:02:42,083 --> 00:02:48,932 which is similar to how we're going to define the author and book factories. 34 00:02:48,932 --> 00:02:50,267 Let's go ahead and 35 00:02:50,267 --> 00:02:56,283 delete the userfactory.php file since we won't be using that file in this project. 36 00:02:56,283 --> 00:03:00,335 Now that we've created the author and book seeders and 37 00:03:00,335 --> 00:03:03,772 added them to the DatabaseSeeder.php file, 38 00:03:03,772 --> 00:03:08,883 we're ready to specify some test data for the AuthorTableSeeder and 39 00:03:08,883 --> 00:03:13,401 BooksTableSeeder files inside of the public run function. 40 00:03:13,401 --> 00:03:18,439 Inside of the public run function, we'll specify values for each column. 41 00:03:18,439 --> 00:03:23,506 However, instead of manually specifying the test data for 42 00:03:23,506 --> 00:03:29,907 each column, we can use model factories and a robust library named Faker. 43 00:03:29,907 --> 00:03:34,455 Next, let's open the AuthorsTableSeeder.php file to take a look 44 00:03:34,455 --> 00:03:39,419 at the public run function inside, which, again, is currently empty. 45 00:03:40,971 --> 00:03:45,291 Inside of the run function, let's add our defined seed data for 46 00:03:45,291 --> 00:03:47,775 each column in our database tables. 47 00:03:47,775 --> 00:03:52,238 First, create two factories, the author factory and 48 00:03:52,238 --> 00:03:56,419 book factory, using these two artisan commands. 49 00:03:56,419 --> 00:04:01,359 Ctrl+K will clear the terminal, 50 00:04:01,359 --> 00:04:07,839 php artisan make:factory AuthorFactory. 51 00:04:13,491 --> 00:04:14,100 And, 52 00:04:32,017 --> 00:04:36,177 Next, open the AuthorFactory.php file and 53 00:04:36,177 --> 00:04:40,775 change Model::class to Author::class, and 54 00:04:40,775 --> 00:04:47,131 modify the import at the top from use App\Model to use App\Author. 55 00:04:49,482 --> 00:04:54,996 Next, let's open the authors_table migration, so we can define the seed 56 00:04:54,996 --> 00:04:59,922 data while looking at the database tables side by side, like this. 57 00:05:27,732 --> 00:05:32,513 Before we move on to defining the book factory, we need to connect the author 58 00:05:32,513 --> 00:05:38,059 factory to the AuthorsTableSeeder file to create three author instances, like this. 59 00:05:44,564 --> 00:05:50,575 Next, let's test our AuthorsTableSeeder using the php artisan db:seed command. 60 00:06:09,838 --> 00:06:13,057 If everything went well, you'd see a message that says, 61 00:06:13,057 --> 00:06:16,435 database seeding completed successfully. 62 00:06:16,435 --> 00:06:17,715 And more importantly, 63 00:06:17,715 --> 00:06:22,120 you'd see that the AuthorsTableSeeder took a little bit longer to complete, 64 00:06:22,120 --> 00:06:27,850 in my case, 0.5 seconds, but this is because we generated some test data. 65 00:06:27,850 --> 00:06:32,470 In comparison, the BooksTableSeeder completed in 0 seconds because 66 00:06:32,470 --> 00:06:34,781 there was no test data to generate. 67 00:06:34,781 --> 00:06:36,332 Way to go. 68 00:06:36,332 --> 00:06:40,367 Next, let's apply the same treatment to 69 00:06:40,367 --> 00:06:46,364 the BookTableSeeder by opening the BookFactory.php file, 70 00:06:46,364 --> 00:06:51,438 then change Model::Class to Book::class, and 71 00:06:51,438 --> 00:06:57,916 modify the import at the top from use App\Model to use App\Book. 72 00:06:57,916 --> 00:07:00,303 Next, let's open the Books table so 73 00:07:00,303 --> 00:07:05,322 we can define the seed data while looking at the database tables side by side. 74 00:07:17,161 --> 00:07:23,187 Finally, we need to connect the book factory to the BooksTableSeeder.php file, 75 00:07:23,187 --> 00:07:28,782 as we did with the AuthorsTableSeeder.php file in the public run function, 76 00:07:28,782 --> 00:07:29,656 like this. 77 00:07:31,731 --> 00:07:32,939 To test our work, 78 00:07:32,939 --> 00:07:38,347 let's use the php artisan migrate fresh command to wipe out the previous data. 79 00:07:46,823 --> 00:07:53,633 Followed by the php artisan db:seed command to reseed our test data. 80 00:07:53,633 --> 00:07:58,130 If all went well, you should see messages that look like this. 81 00:07:58,130 --> 00:07:58,840 Way to go