Intro to Models3:14 with Jonathan Barrios
Now that we’ve covered Routing, Controllers, and Views, let’s talk about the final element: Models. Models are the direct connection between a database and the Laravel application. In a perfect world, Models will directly mirror the structure of your database. That is to say, each database table corresponds to a model-- the user model interacts with the user table, and so on. Let’s look at how we can connect our Laravel application to the database.
[MUSIC] 0:00 [SOUND] Welcome back. 0:03 Now that we've covered routing, controllers and views, 0:06 let's talk about the final element, models. 0:09 Models are the direct connection between the database and the Laravel application. 0:14 In a perfect world, models will directly mirror the structure of your database. 0:19 That is to say, each database table corresponds to a model. 0:24 The user model interacts with the user table, and so on. 0:29 Let's take a look at how we can connect our Laravel application to the database. 0:33 To start connecting our database, we need to delete the default migrations in 0:39 the Database forward slash Migrations directory. 0:43 Then create a model using the php artisan make model course -m command. 0:46 The dash m flag tells Laravel to create a migration for us, 0:56 which really speeds up development and course is the name for the model. 1:00 After we create the model and migration, we need to update the current default 1:05 migration schema in the database forward slash migrations directory. 1:10 This is what the default schema looks like. 1:15 As you can see, the default schema is very minimal and 1:18 does not contain the data types we will need for our application. 1:22 Let's take a look at the completed Laravel app. 1:26 And each of the data types that we need to add to our default schema. 1:29 As you can see, we have a title, summary, duration, language, 1:33 and difficulty displayed for each course in our library. 1:39 Next, let's add new fields for our tables in order for our application 1:45 to handle the new data types such as string, integer and long text. 1:50 In the data base facade, you will see each data type represented such as the title, 1:55 followed by a summary of long text as well as an integer for duration, 2:00 a string for language in difficulty and another integer for number of stages. 2:05 Let's modify the default schema inside of the database forward slash 2:10 migrations directory and the file that ends with create courses tables.php. 2:15 Once inside of this file, 2:25 modify the public function named up using the project files like this. 2:27 With the updated schema, our app will be able to migrate and 2:38 see the correct data with very little effort on our part. 2:42 If you're not familiar with the up and down functions, 2:46 check the teachers notes below for more information about migration class methods. 2:49 Now that we've set up our database, we'll give our app control of the database and 2:55 its structure by using migrations, which we'll cover in an upcoming video. 3:00 And the next section, we'll explore Laravel's built in 3:05 PHP tool Tinker to quickly read data from our database. 3:09 See you in the next section. 3:13
You need to sign up for Treehouse in order to download course files.Sign up