Why Use Migrations 0:00Database migrations are one of Laravel's most powerful features. So a migration can seem a little daunting at first if you're not familiar with the term. Here's how I think of it. They are version control for your database. So in the past, imagine that you needed some kind of table. Maybe a table for articles. Well, how did you create that? And here's what I bet you did. I bet you opened up some kind of GUI app. Maybe SQL Pro or one of its companions. And then you manually created the table and defined the columns. Okay, well, no problem with that. But now what about when you add a second person to your team? How did they get your database set up? Well, I guess you export the schema and you send that over to them and then they import it. But now what about when you make another database change? Maybe we're changing a column name. Well, once again, we have theseschema and you send that over to them and then they import it. But now what about when you make another database change? Maybe we're changing a column name. Well, once again, we have these consistency issues where all of the developers on your team, especially if you're working remotely, need to make sure that their database setup is consistent with one another. So as you can imagine, this can get a little tricky. Not to mention the fact that your production database needs to be synced as well. So what if instead we could represent these sorts of instructions to our database as a PHP class? What sort of benefits would that bring? Let me show you. We have a couple that come straight out of the box. Go to database, migrations, and you'll see that we have a couple here included. One for creating a users table and another for password resets. Now remember, this sort of stuff, if you don't have a user Understanding Up and Down 1:28migrations, and you'll see that we have a couple here included. One for creating a users table and another for password resets. Now remember, this sort of stuff, if you don't have a user system in your application, delete it. It doesn't need to be there. However, most apps will have a users table, so this can be really useful. Now, if immediately you're a little overwhelmed and you think, oh, I don't want to create a class every time I make a change to my database, that'll take too long. Actually, no. Laravel has some generators that'll speed things up quite a bit. So with that in mind, let's just take a look. We have two methods, up and down. So this means for our up method, what sort of action do we want to take place? In this case, we are creating a new table called users. And as for the blueprint of the table, well, it needs an auto-incrementing ID, a name that is a string, an email that's a string and also is unique, a password that is 60 characters,users. And as for the blueprint of the table, well, it needs an auto-incrementing ID, a name that is a string, an email that's a string and also is unique, a password that is 60 characters, a remember token. It's a little special. Don't worry about it too much right now. And then some timestamps. When was this user created? When were they last updated? But now, what about the down method? Well, here's one of the great things about a migration. Let's say you run a migration to create this users table, but then 20 minutes later you realize, oh, it shouldn't be name. It's supposed to be username. Well, you can fix that really easily now. All you'd have to do is roll back the migration. I'll show you how to do that. You make your change and then you rerun the migration and you're done. Nothing else to do there. And even better, these sorts of changes and modifications are part of your version control, Running Migrations Locally 3:02make your change and then you rerun the migration and you're done. Nothing else to do there. And even better, these sorts of changes and modifications are part of your version control, which is very cool. All right, but let's just leave it with name. And now we have a migration here, but does that mean we already have a users table? No, we've created the class, but we haven't yet migrated the database. Let's try it out. php artisan migrate. And there we go. We created a users table and a password resets. So now let's dig in and see what that looks like. And I'm going to assume that you already have SQLite installed on your machine. You probably do, but if you don't, you can verify it by running SQLite 3, and you should see a prompt like that. Otherwise, if you're on a Mac, check something out called Homebrew, which would allow you to do Homebrew install SQLite. Very useful, and that would apply toprompt like that. Otherwise, if you're on a Mac, check something out called Homebrew, which would allow you to do Homebrew install SQLite. Very useful, and that would apply to any number of little tools and components that you can pull in. And of course, if you're going through all of this and you just can't get it set up on your machine, leave a comment below, and a bunch of us will try to help you. All right, so let's try it out. SQLite 3, and I will specify a path to the file. All right, let's review the tables, and you'll see we have a migrations table. You never need to touch that. The Laravel framework uses that to determine if there's new migration classes to run, what we need to roll back, stuff like that. But next, we have the two tables we created. Let's run the schema for these, and you'll see a general bit of SQL. Create table users, and all the fields there were generated by Laravel based upon that migration class thattables we created. Let's run the schema for these, and you'll see a general bit of SQL. Create table users, and all the fields there were generated by Laravel based upon that migration class that we created. Now, let's go back to that example where we said, well, what if we called it name, but it should really be username? What is the workflow to fix that? Well, I'm going to open a new tab, and I'll say php artisan. Well, first, let me show you. If I run that, you can see all the various commands that you can run. Mostly, though, you're going to be using these three here. So I want to roll back the last migration. php artisan, migrate, roll back. Think of that as sort of your undo. I just ran a migration, but I made a mistake, so I'm going to roll it back, fix it, and then rerun it. So now, we fix it. We're going to change this to username, come back, and run php artisan, migrate, and we're finished. The database has been updated. Just to prove it Generating New Migrations 5:24fix it, and then rerun it. So now, we fix it. We're going to change this to username, come back, and run php artisan, migrate, and we're finished. The database has been updated. Just to prove it to you, we can review the schema again, and now you'll see it's reflected there. Pretty cool, right? So now that you understand how to run migrations and roll them back, what about creating new ones? Okay, let's continue with that basic idea of an article. We want a table to store all of the articles for your blog. Very simple. Well, we can say php artisan, and I want to make a new migration. Now, don't forget, if you ever forget the sequence or the arguments that you need to provide, just prepend help. The important one is right here, the name of the migration. Think of that as an identifier that describes what the migration does. So if I'm creating an articles table, then the name could be create articles table. Next, you'll see we have a couple flags hereas an identifier that describes what the migration does. So if I'm creating an articles table, then the name could be create articles table. Next, you'll see we have a couple flags here that simply adjust the boilerplate that will be used when your file is generated. So let's try this out. php artisan make migration create articles table. And I am creating a table, so I will pass create articles. All right, let's run that. And it created this new migration. Let's review it. In the sidebar, you'll see we have a new one here. And even better, it's done a bunch of boilerplate for us. It's going to assume that you want an auto-incrementing ID, pretty typical for a table. And also a created at and updated at timestamp would be useful as well. Typically, it's best to include those unless you have a good reason why they're not necessary. So let's think about this. The most basic article would have a title and the body.useful as well. Typically, it's best to include those unless you have a good reason why they're not necessary. So let's think about this. The most basic article would have a title and the body. How about that? Okay, well, table, we have a string that will be the title. And then another one that will be not a string, but a full text field for the body. And who knows, you might want other things such as a published at timestamp. Okay, table, timestamp, published at. That way, you could have some logic that says, I can schedule an article, but unless the published at date is today or at some point in the past, do not display that article. Okay, so that's how we create the table. For the down method, that's how we undo the creation. And in this case, we would drop the articles. But in other cases, you're just modifying the table. So in your down method, you would undo the modification. And I'll show you what that Modifying Tables Safely 8:01this case, we would drop the articles. But in other cases, you're just modifying the table. So in your down method, you would undo the modification. And I'll show you what that looks like shortly. For now, the migration looks good. So let's migrate the database. php artisan migrate. And you're done, you have a new table there. So next, if you needed to make a change, and especially if you haven't pushed to production yet, my recommendation is just roll it back, make your adjustments and rerun it. But let's imagine you've already pushed to production, and you can't do that anymore. So now we've decided that we need to add another column to this table, we can create a new migration to do that. Like this, php artisan make migration. And once again, the ID here or the name of the migration, all you have to do is just describe exactly what you're doing. And that's a good convention to use.php artisan make migration. And once again, the ID here or the name of the migration, all you have to do is just describe exactly what you're doing. And that's a good convention to use. Why don't we say that for your article, you also want to add a new column, maybe something like an excerpt. So the excerpt is what displays on the overview page. And when you click through to the article, you see the full body. Alright, so we want to add an excerpt to the articles table, add excerpt to articles table. Done. So let's see what that one looks like. Now, we have a new one. But this is a little different. And notice that in this case, we don't have quite as much boilerplate. So yes, we could write it out manually wouldn't take too much time. But if we can make Laravel do more work for us, why wouldn't we? So let's run it again. But this time, I'm going to tell Laravel which table I'm working with table is articles. Okay, now, because we included that,do more work for us, why wouldn't we? So let's run it again. But this time, I'm going to tell Laravel which table I'm working with table is articles. Okay, now, because we included that, we get just a little bit more boilerplate. Alright, we make our change. So I want to add a new text column for the excerpt. And now if I want to undo that, then we would drop the column table, drop column, excerpt. And that's the formula, so to speak, for how we undo what we did up here. Okay, I think we're ready to run this. But one last thing, would it be okay for our table to have an excerpt column with nothing in it? Or in other words, when you create a new article, is it okay if the excerpt is null? And you might decide yes, that would be okay. If so, then you want to add nullable. PHP artisan migrate. And we're done. We now have an excerpt column on our articles table. Let's make sure we run it. We review the schema. And right down here Rollback Gotcha: Doctrine 10:36If so, then you want to add nullable. PHP artisan migrate. And we're done. We now have an excerpt column on our articles table. Let's make sure we run it. We review the schema. And right down here at the bottom is our excerpt. But now there's one little gotcha that I want you to be aware of. Let's roll it back just for fun. Let's see what happens. PHP artisan migrate rollback. And crap, everything failed. So if we scroll up, we can see that Laravel is trying to reference this class, but it couldn't find it. And that's because if you want to drop a column, well, we require a special package. And the Laravel documentation will tell you everything you need. All you need to do is say composer require doctrine slash dgal. So let's pull that in and give it just a second to install. There we go. Let's try it again. Roll back the migration. And now it works. So just keep that in mind. And once again, if we run the schema for it, we will see that reflected here. There'sinstall. There we go. Let's try it again. Roll back the migration. And now it works. So just keep that in mind. And once again, if we run the schema for it, we will see that reflected here. There's no excerpt. All right. So now that you understand the fundamentals of migrations, in the next episode, I get to introduce you to Eloquent, which you're going to love.