1 00:00:00,750 --> 00:00:05,691 Most applications connect to a database and use the existing structure or schema. 2 00:00:05,691 --> 00:00:09,405 However, this can lead to problems during development, 3 00:00:09,405 --> 00:00:14,043 since the same database structure for each instance cannot be ensured. 4 00:00:14,043 --> 00:00:18,318 Laravel solves this development challenge by using migrations, 5 00:00:18,318 --> 00:00:22,374 which allow team members to share the app's database schema. 6 00:00:22,374 --> 00:00:27,941 Migrations are instructions that Laravel gives to the database to create tables, 7 00:00:27,941 --> 00:00:31,916 foreign key constraints, like strings and integers, and 8 00:00:31,916 --> 00:00:35,349 everything else you might need for your database. 9 00:00:35,349 --> 00:00:41,463 With one command, every developer will have an identical database problem solved. 10 00:00:41,463 --> 00:00:47,658 We already created our model using the php artisan make model command. 11 00:00:47,658 --> 00:00:52,584 And now, we're ready to deploy a migration, let's check it out. 12 00:00:52,584 --> 00:00:57,570 To get started, let's make our Seeder using the php artisan 13 00:00:57,570 --> 00:01:01,202 make Seeder CoursesTableSeeder command. 14 00:01:01,202 --> 00:01:04,412 CoursesTableSeeder is the name of the Seeder and 15 00:01:04,412 --> 00:01:09,107 follows the best practice of mirroring the name of the model as the first 16 00:01:09,107 --> 00:01:11,782 word followed by TableSeeder as shown. 17 00:01:11,782 --> 00:01:16,226 Instead of the newly created CoursesTableSeeder.php file, 18 00:01:16,226 --> 00:01:19,929 you'll notice an empty public function named run. 19 00:01:19,929 --> 00:01:25,185 This is where we will add our database records, which will seed our database. 20 00:01:25,185 --> 00:01:30,456 To add the database records for our courses, download the project files and 21 00:01:30,456 --> 00:01:34,325 add the contents of the run function into your project, 22 00:01:34,325 --> 00:01:39,450 inside of the newly created CoursesTableSeeder.php file, like this. 23 00:01:45,758 --> 00:01:49,392 Before we continue, let's make sure the course model and 24 00:01:49,392 --> 00:01:53,556 the database class are available to us by adding two lines of code. 25 00:01:53,556 --> 00:01:58,306 First, add use Illuminate\Support\Facades\DB in 26 00:01:58,306 --> 00:02:05,992 the DatabaseSeeder.php file just under use Illuminate\Database\Seeder on line 4. 27 00:02:05,992 --> 00:02:11,471 To learn more about the database class facade, check the teachers notes below. 28 00:02:11,471 --> 00:02:15,449 Next, let's add use App\Course, on line 4, 29 00:02:15,449 --> 00:02:22,255 under Illuminate\Http\Request inside of the AppController file, like this. 30 00:02:29,712 --> 00:02:33,480 Finally, head over to the DatabaseSeeder.php file in 31 00:02:33,480 --> 00:02:35,966 the migration/seeds directory and 32 00:02:35,966 --> 00:02:39,985 change the table name inside of the run function like this. 33 00:02:49,264 --> 00:02:54,402 Now, we're ready to migrate and seed the database using the php 34 00:02:54,402 --> 00:02:59,368 artisan migrate and php artisan db: seed command like this. 35 00:03:18,567 --> 00:03:19,132 Nice job. 36 00:03:19,132 --> 00:03:24,069 We've migrated and seeded the database, which means, we can use Tinker to 37 00:03:24,069 --> 00:03:28,094 verify that our database records are indeed in the database. 38 00:03:47,127 --> 00:03:49,941 And here are all of our database records, and 39 00:03:49,941 --> 00:03:52,988 this is what makes Tinker such a wonderful tool. 40 00:03:54,387 --> 00:03:58,579 To exit the Tinker environment, simply type exit. 41 00:03:58,579 --> 00:04:02,074 On a Mac, you can use Ctrl + K to clear the terminal. 42 00:04:02,074 --> 00:04:05,770 We only have one more step to complete before we can see our 43 00:04:05,770 --> 00:04:10,093 app displaying all of the database records as Treehouse courses. 44 00:04:10,093 --> 00:04:13,642 Head over to the library.blade.php file and 45 00:04:13,642 --> 00:04:17,832 add the foreach loop from the project files like this. 46 00:04:50,637 --> 00:04:54,705 Add the foreach loop before the penultimate div tag like this. 47 00:04:54,705 --> 00:05:00,058 Save your changes and use the php artisan serve -- host = 48 00:05:00,058 --> 00:05:05,968 localhost command to view the app using the artisan and server. 49 00:05:05,968 --> 00:05:09,007 Like magic, we have a Treehouse course catalogue. 50 00:05:12,814 --> 00:05:15,828 At first glance, models may appear simple and 51 00:05:15,828 --> 00:05:19,542 that's because at this stage, they are, here's why. 52 00:05:19,542 --> 00:05:25,011 So far, models directly mirror our database, one model for each table. 53 00:05:25,011 --> 00:05:29,543 Of course, there are a lot more advanced techniques you can apply to models, 54 00:05:29,543 --> 00:05:34,285 like using Eloquent to work with database objects and their relationships, but 55 00:05:34,285 --> 00:05:36,953 that's outside of the scope of this course. 56 00:05:36,953 --> 00:05:42,492 If you're curious about how to apply your PDO knowledge to a Laravel application or 57 00:05:42,492 --> 00:05:47,002 to learn more about working with database objects using Eloquent, 58 00:05:47,002 --> 00:05:49,472 check out the teachers notes below. 59 00:05:49,472 --> 00:05:50,600 In this course, 60 00:05:50,600 --> 00:05:55,931 we built the Treehouse course library together using the Laravel framework. 61 00:05:55,931 --> 00:05:58,999 We created a Laravel app using composer, 62 00:05:58,999 --> 00:06:03,559 connected the database by updating the ENV credentials, and 63 00:06:03,559 --> 00:06:08,132 finally, created routes for each page of your application. 64 00:06:08,132 --> 00:06:13,596 We also created DRY views using @include and @extends to display 65 00:06:13,596 --> 00:06:18,598 style of web pages, including the Treehouse course catalog. 66 00:06:18,598 --> 00:06:23,410 You now have the foundational knowledge to begin building your 67 00:06:23,410 --> 00:06:25,961 very own Laravel applications. 68 00:06:25,961 --> 00:06:30,143 But there's a whole lot more to learn when it comes to mastering Laravel. 69 00:06:30,143 --> 00:06:35,387 We're here to help you succeed, If you have any questions about this course, 70 00:06:35,387 --> 00:06:38,485 feel free to reach out to the Treehouse staff or 71 00:06:38,485 --> 00:06:42,870 other students in the community, and as always, happy coding.