Deploying Migrations6:43 with Jonathan Barrios
Most applications connect to a database and use the existing structure or schema but this can lead to problems during development since the same database structure for each instance cannot be ensured. Laravel solves this development challenge by using Migrations, which allows team members to share the app's database schema. Migrations are instructions that Laravel gives to the database to create tables, foreign key constraints like ‘STRING’, ‘INTEGERS’, and everything else you might need for your database. With one command, every developer will have an identical database, problem solved. We already created our Model using the php artisan make:model command and now we’re ready to deploy our migration! Let’s check it out.
Most applications connect to a database and use the existing structure or schema. 0:00 However, this can lead to problems during development, 0:05 since the same database structure for each instance cannot be ensured. 0:09 Laravel solves this development challenge by using migrations, 0:14 which allow team members to share the app's database schema. 0:18 Migrations are instructions that Laravel gives to the database to create tables, 0:22 foreign key constraints, like strings and integers, and 0:27 everything else you might need for your database. 0:31 With one command, every developer will have an identical database problem solved. 0:35 We already created our model using the php artisan make model command. 0:41 And now, we're ready to deploy a migration, let's check it out. 0:47 To get started, let's make our Seeder using the php artisan 0:52 make Seeder CoursesTableSeeder command. 0:57 CoursesTableSeeder is the name of the Seeder and 1:01 follows the best practice of mirroring the name of the model as the first 1:04 word followed by TableSeeder as shown. 1:09 Instead of the newly created CoursesTableSeeder.php file, 1:11 you'll notice an empty public function named run. 1:16 This is where we will add our database records, which will seed our database. 1:19 To add the database records for our courses, download the project files and 1:25 add the contents of the run function into your project, 1:30 inside of the newly created CoursesTableSeeder.php file, like this. 1:34 Before we continue, let's make sure the course model and 1:45 the database class are available to us by adding two lines of code. 1:49 First, add use Illuminate\Support\Facades\DB in 1:53 the DatabaseSeeder.php file just under use Illuminate\Database\Seeder on line 4. 1:58 To learn more about the database class facade, check the teachers notes below. 2:05 Next, let's add use App\Course, on line 4, 2:11 under Illuminate\Http\Request inside of the AppController file, like this. 2:15 Finally, head over to the DatabaseSeeder.php file in 2:29 the migration/seeds directory and 2:33 change the table name inside of the run function like this. 2:35 Now, we're ready to migrate and seed the database using the php 2:49 artisan migrate and php artisan db: seed command like this. 2:54 Nice job. 3:18 We've migrated and seeded the database, which means, we can use Tinker to 3:19 verify that our database records are indeed in the database. 3:24 And here are all of our database records, and 3:47 this is what makes Tinker such a wonderful tool. 3:49 To exit the Tinker environment, simply type exit. 3:54 On a Mac, you can use Ctrl + K to clear the terminal. 3:58 We only have one more step to complete before we can see our 4:02 app displaying all of the database records as Treehouse courses. 4:05 Head over to the library.blade.php file and 4:10 add the foreach loop from the project files like this. 4:13 Add the foreach loop before the penultimate div tag like this. 4:50 Save your changes and use the php artisan serve -- host = 4:54 localhost command to view the app using the artisan and server. 5:00 Like magic, we have a Treehouse course catalogue. 5:05 At first glance, models may appear simple and 5:12 that's because at this stage, they are, here's why. 5:15 So far, models directly mirror our database, one model for each table. 5:19 Of course, there are a lot more advanced techniques you can apply to models, 5:25 like using Eloquent to work with database objects and their relationships, but 5:29 that's outside of the scope of this course. 5:34 If you're curious about how to apply your PDO knowledge to a Laravel application or 5:36 to learn more about working with database objects using Eloquent, 5:42 check out the teachers notes below. 5:47 In this course, 5:49 we built the Treehouse course library together using the Laravel framework. 5:50 We created a Laravel app using composer, 5:55 connected the database by updating the ENV credentials, and 5:58 finally, created routes for each page of your application. 6:03 We also created DRY views using @include and @extends to display 6:08 style of web pages, including the Treehouse course catalog. 6:13 You now have the foundational knowledge to begin building your 6:18 very own Laravel applications. 6:23 But there's a whole lot more to learn when it comes to mastering Laravel. 6:25 We're here to help you succeed, If you have any questions about this course, 6:30 feel free to reach out to the Treehouse staff or 6:35 other students in the community, and as always, happy coding. 6:38
You need to sign up for Treehouse in order to download course files.Sign up