Creating a Migration 0:00Now, in a recent episode, we manually created this post table. But as it turns out, you'll rarely do this. So let's go ahead and delete it entirely. I'll hit Command-S to commit the change, and then we'll start from scratch. Instead, we'll use a migration to build up the table. Like this. PHP Artisan, make a migration, and now we're going to give it a readable name. If you're creating a table, the convention is usually create, table name, table. If you're updating a table, you might say UpdateTitleOnPostTable, and that's the name you give it. What Migrations Are 0:29If you're updating a table, you might say UpdateTitleOnPostTable, and that's the name you give it. So let's run that, and you'll see it creates a new class here that we can then edit. Now you'll find these in your database directory. Out of the box, Laravel includes a few that you will almost always want. For example, a user stable, a password reset stable, and a failed job stable. We'll take a look at those later, but for now, here's the new one we created. Now think of a migration as version control for your database. It gives us a programmatic way to define the structure of a table, or even make a change to an existing one. Defining Table Columns 0:58It gives us a programmatic way to define the structure of a table, or even make a change to an existing one. And what's great about this is if at any point we make a mistake or we need a change, all we have to do is roll back the migration, and then rerun it. Or even better, because these files are under version control, someone else on your team can clone in the project, they run their migrations, and they instantly have the exact same database structure. So let's reproduce what we had earlier. We need a slug for the post. We'll do a simple string there.We need a slug for the post. We'll do a simple string there. And we also need the body. And again that will be text. Next you'll see this section for timestamps, and it's included by default. This will add created at and updated at timestamps to the table. However, let's add one more timestamp. Often you will want to schedule a post for a future date. So let's add a timestamp for the published at column. But if you want to declare that a column is nullable, you can add it here.So let's add a timestamp for the published at column. But if you want to declare that a column is nullable, you can add it here. This is our way of saying when you prepare a new post, the published at column is optional. And you can set that at a later date. Okay, so notice we have an up and down method. Up means moving forward when you run your migrations. Down is when you roll back or you undo the migration. So in this case, to move forward, we create the table. If we roll it back, we drop the table. If you have another example where a migration adds, how about a title column? Running the Migration 2:22If we roll it back, we drop the table. If you have another example where a migration adds, how about a title column? Okay, forward would be to add that column. Down would be to drop the column. Or in other words, the down method should undo anything you've done here. All right, we're all set to go. I can run my migration like this, php artisan migrate. Now you're going to see a handful here. And again, that's because out of the box, Laravel includes a few migrations to get you started. Adding Columns via Migrations 2:48And again, that's because out of the box, Laravel includes a few migrations to get you started. But here's the one we care about. So if I now switch back to TablePlus and we refresh, here's all the tables that were created, including ours. So let's finish up by discussing how you might add an additional column. And I'm going to show you two ways, one being the recommended approach. So you might think, I need a title on that table. So we create a brand new migration. php artisan make migration, add title to post table.So we create a brand new migration. php artisan make migration, add title to post table. All right, let's visit that file. And this time, I'm going to say table string for the title. And if we roll it back, I drop the column. Table drop column title. Great. So we have a new migration to add a column to the post table. I will run it yet again, php artisan migrate. And this time if I go back to TablePlus and I refresh, sure enough, we do have that new Development vs Production Workflow 3:40I will run it yet again, php artisan migrate. And this time if I go back to TablePlus and I refresh, sure enough, we do have that new title column. And here's the rule. This is the approach you want to take after you've pushed the code to production. However, before that, when you're still in development mode, it doesn't really make much sense. So here's what we'll do. I'm going to roll back that last migration using migrate rollback. And don't forget, by the way, if you ever forget, run php artisan.I'm going to roll back that last migration using migrate rollback. And don't forget, by the way, if you ever forget, run php artisan. And if I scroll up, you'll see all of the various migrate commands you can run. In our case, we reversed that most recent migration. So if I refresh again, the title column is now done. Now I'm going to return to my editor and I'm going to delete this migration. Instead, if I need to make a change, I can return to that original migration. We'll do it right here. Table string title. Okay.Table string title. Okay. But now be careful. You've made a change. So that means if you run php artisan migrate, it'll let you know, hmm, I already ran this file. So you have two choices. One would be to run php artisan migrate rollback. So now we roll back the creation of the post table and then we rerun it. But just be careful.So now we roll back the creation of the post table and then we rerun it. But just be careful. Every time you do this, of course, you lose all of the data. And that's why if you're in production mode, be very, very careful. Anyhow, now if we refresh, I have that title column. So you could take that approach. Or if all the tables are empty, you're free to run php artisan migrate fresh. What this will do is drop all of your tables and then rerun everything from scratch. And that would work as well.And that would work as well.