4.1 Creating Migrations and Models
We typically don't use management tools to work with our database after it is created. Instead, we use migrations. In this lesson, we'll build our guitars table and create the model.
1.Introduction2 lessons, 07:30
2.Basic Routing5 lessons, 40:40
3.The Blade Templating Engine7 lessons, 45:30
4.Working With Data6 lessons, 48:45
5.Conclusion1 lesson, 01:03
4.1 Creating Migrations and Models
As I mentioned in the previous lesson, we typically don't use our database management tools for creating and working with our tables. Instead we use artisan to create and run what we call migrations. Now, migrations are really nothing more than a way of versioning a database. And it's really not a development tool as it is a production tool. It gives us the ability to not only make changes to our database safely, but we can also roll back those changes if we ever decide to. So really there's going to be two things that we need to do, we need to create a migration, which is for changing the structure of the database itself. So that's creating a table, it's adding columns, it's things like that. We also need to create what's called a model. This is the code that we use that actually interacts and works with the data inside of the database. So let's look at how we can do that. We will of course use artisan, and anytime we want to make something we are going to prepend the command with make. And in this case migration let's look at the help. Now, at the very least, you have to provide a name and most of the time that's going to be it. But one thing to be aware of is that your names need to be specific, such as create guitars table. Or if you are going to add a column, then you would say add years column to guitars. It can get very long but the reason is because it needs to be specific as to what it does. Because as you'll see here in a few moments, there's a lot of migrations. So if you're vague in the names of your migrations it's gonna get confusing. So just be specific there, so that's how you would create a migration, let's look at creating a model. So the command is make model at the very least you have to provide a name. The name of a model is the name of the class that is going to be used, so this is much easier than creating a migration. Now in our case, we want to create a table but we also need to create a model that's going to essentially interact with that table. So we need to do both of these things. They are related to one another, so it would be nice if we could create them at the same time and we can, by using the migration flag. So this is what we're going to do, we'll use the make model command. We're going to call our model guitar, by convention our model names are singular nouns. So if we needed a table to store people's automobiles like cars, we would just call it car. Or if we were building some kind of real estate application to where we needed to store individual properties, we would call it property. Something along those lines, in our case is just going to be guitar. And then we want to specify the migration flag. And then whenever that runs, let's go to our project. Let's start by going to the database folder and then migrations and you can see that there are already some migrations here. This is why I said that we need to be specific as to the name of our migrations. Because if you just name them vague like migration one, migration two, migration three, you don't really know what's going on here. So, here we can see the create guitars table that was named automatically by artisan because, well, that's what we're doing. We are creating a table, so let's open up this file, and you're going to see that it's really nothing more than a class, and it has two methods. The first is up, the second is down. The up method is for doing whatever this migration is for, so since this is for creating a guitars table, that's what is inside of the up method. Now, remember I said that you can roll back a migration and that's what the down method is for, is to undo it whatever was done inside of the up method. Now, a note of caution, you only want to undo whatever it is that you did inside of up. So, in this case this is creating guitars the down method is going to drop guitars if it exists, it's not going to do anything else on any other table. It's just going to undo what up did, and if you try to do more than that, you can start running into issues. Now, let's focus on the code that is creating this guitars table. You can see that it's using a builder type of syntax. We're not really defining columns here, we're just building a table using normal PHP code. So, the first is id, this is going to create a column that contains a unique identifier for every record. I am a firm believer in that every table that you create should have an id. Even if you don't think you need one, you need one, so, just always include the id. Storage is cheap, and ids by default are integer values. So they aren't going to take up a lot of space anyway, so just include it. You'll save yourself a lot of headache later. Then there's the timestamps, and this is a very nice little feature, this adds two columns. The first is going to contain the date and time that a record was created. And the second column is the date and time that the record was last modified. And that sounds like a lot of work, but we don't have to do it, it's done automatically, so that's nice to have. So let's build the rest of our guitars table because we've been working with this data already. So we're going to use this table object here and we want to add a column for the name of the guitar, which is going to be a string value. So we'll pass in the name of the column that we want, so let's just call that name. Let's copy and paste that because we also need the brand of of the guitar, but let's also include the year that the guitar was made. Now, we could use a string, but that makes no sense whatsoever. So we're gonna make this an integer value because that's what a year is, it's an integer. And then as far as the name of the column is concerned, we could say year, but let's say year made. Now, by convention we use an underscore to separate names. Of course, you can follow whatever convention that you want, just be consistent in what you do. And of course if you needed other columns you can add those. There are a ton of methods for building a table, we're not going to go through them because we don't have that kind of time. So, there will be a link in the description for this video that will take you to the documentation of all of the methods that will help you build a table. In our case, this is all that we need, so we're done here. So, before we run this migration, let's look at our model, that's inside of the app and then the models folder and then there's this guitar,php. This is nothing more than a class and that's a very simple class. There are some things that we will add to this class, but for right now, this is going to be just fine. And one other thing that I want to point out is that this guitar extends model. And if we look at model, it's inside of a folder called eloquent. Eloquent is what's called an object relational mapping or ORM. This is a very nice way of working with the data within a relational database. Because at the end of the day, what we get to work with is just normal PHP code. We don't have to write raw SQL, although in some cases you might need to, but in most cases you don't. So Eloquent is the ORM that we use to interact with our data. And so this guitar is an eloquent model. So, before we close up this lesson, let's go back to the command line and we want to run the migration. So, if we look at migrate, that is the command that we would use to run all of the migrations that haven't been run yet which, well, there are several, there's one, two, three, four and then ours is the fifth. So, it's going to run all of those migrations, but it's also going to keep track of the migrations that haven't been ran. But really, that's not what I wanted to show you. Let's just run PHP artisan, because migrate has some very useful things such as fresh which is going to essentially drop all of the tables and it will rerun all of the migrations. So as you're developing, and you find that you just need to start fresh, you can use migrate fresh. And then that's going to start you over at least as far as the database is concerned. But then there's the reset, which is going to roll back all of the database migrations, or you can roll back just the last database migration. In our case, we want to just run the migrate command. Because that is going to run our migrations, you're going to see that it ran all of those migrations. And if we look at our database management tool, we're going to see those tables. Of course, the one that we are concerned with is guitars, right there. And if we look at this database table, there's of course not going to be any data. But we can at least see the file structure where we have the id, the name, the brand, the year made, the created at and the updated at columns. So in the next lesson, we are going to create a form that we will use to create and store data in our guitars table.