---
title: 'How to Migrate Data in Laravel'
source: 'https://youtube.com/watch?v=XMjzWO-E_dI'
video_id: 'XMjzWO-E_dI'
date: 2026-08-11
duration_sec: 1010
---

# How to Migrate Data in Laravel

> Source: [How to Migrate Data in Laravel](https://youtube.com/watch?v=XMjzWO-E_dI)

## Summary

This tutorial episode from the Lerowell series demonstrates how to create and manage database tables in Laravel using migrations. It covers the structure of migration files, adding columns, running migrations, and rolling back changes, as well as the essential step of defining fillable fields in the model for mass assignment.

### Key Points

- **Introduction and Context** [00:00] — The video continues from a previous episode where a model and controller were created. The goal is to learn how to migrate information into the database, specifically creating a 'posts' table.
- **Migration File Creation** [00:16] — When the model and controller were created, a migration file was automatically generated in the 'database/migrations' folder with a timestamp. This file contains the blueprint for the posts table.
- **Understanding Migration Structure** [01:30] — The migration file contains a class extending 'Migration' with 'up' and 'down' methods. The 'up' method creates the table, while the 'down' method reverses it (e.g., deletes the table).
- **Default Columns** [03:11] — The default migration includes an auto-incrementing 'id' and timestamps. These are not enough for a posting system, so additional columns like 'title' and 'body' need to be added.
- **Adding Columns** [04:21] — To add a column, use '$table->string('title')' for a string and '$table->text('body')' for longer text. The data type is determined by the method called on the table builder.
- **Running Migrations** [06:01] — Run 'php artisan migrate' in the terminal to apply pending migrations. This creates the 'posts' table in the database with the specified columns.
- **Verifying Table Creation** [07:39] — After migrating, refreshing the database shows the 'posts' table with columns: id, title, body, created_at, and updated_at. The timestamps are automatically split into two columns.
- **Rolling Back Migrations** [08:34] — Use 'php artisan migrate:rollback' to revert the latest migration. This deletes the 'posts' table. To reapply, run 'php artisan migrate' again.
- **Targeting Specific Migrations** [09:21] — If you need to roll back a specific migration (not the latest), use 'php artisan migrate:rollback --path=database/migrations/filename.php'. Similarly, 'php artisan migrate --path=...' reruns a specific migration.
- **Refreshing All Migrations** [11:56] — The command 'php artisan migrate:refresh' runs all migrations from scratch, recreating all tables. This is useful for a clean slate.
- **Mass Assignment Protection** [12:48] — To allow inserting data into the posts table, the model must define a '$fillable' property listing the columns that can be mass-assigned. This is a security feature to prevent unauthorized field updates.
- **Defining Fillable Fields** [14:18] — In the Post model, add 'protected $fillable = ['title', 'body'];'. Only these fields need to be fillable because 'id' and timestamps are auto-generated.

### Conclusion

By the end of this episode, viewers know how to create, modify, and revert database tables using Laravel migrations, and how to secure their models with mass assignment protection. This sets the foundation for building a CRUD system in the next episode.

## Transcript

So welcome back to our next episode of Lerowell. We're going to learn how to migrate information inside our database because that is something we have not done yet. Again, this is kind of a continuation of the previous video where we learned how to create a model and a controller.
So right now we do have a post.php file, which is our model, and then we also have a post controller. Now, currently inside our database, we do not have a post table because that is something that
we need in order to actually contain all the information for our posts inside this website here. So how do we create that? Well, when we created our model and controller, we actually
created another file. So if we were to actually go inside where I have database inside our main root directory of our project here, you can see that we have a folder called migrations.
Now, inside this one, we actually have one that has a date timestamp, which is set to the current date that I have. Currently, as I'm making this tutorial here.
And this is because this file was created when I created the model and controller inside the terminal. So if I open up this migration file, you can see that we have some information. And this is actually information about how to create basic data for a posting table inside our database.
And as you can see, we do actually, if I just kind of like take whatever we have inside here a little bit at a time so you know what is in here, we have a new class that extends migration, which is used to migrate.
Migrate is basically a, I wouldn't call it a fancy word, but it's a word for us on how to send information to our database and tell it what to do, basically.
And there's actually many different versions. You can see that we have inside our migrations folder here, like there's a couple of different ones going down. And what is one of the cool things is that you can actually revert some of these changes if you decide, oh, okay, well, this change messed up something.
So, therefore, we need to be able to revert a change or something, or maybe delete some of the changes that we did. Well, that is actually also built into LevelWorld, which, you know, is something we can actually do.
So, inside this file here, you can see that inside this class that was created, we have two different methods. We have a up method, and we have a down method. Now, the down method is basically for reversing the migration.
So if I, let's say, created a table inside my database using the up method, then I can delete the table again using the down method. So it's kind of like a, you know, create something, oh, okay, we'll delete it again sort of thing.
So this one down here, the down method, isn't really that important right now. You can just basically see that it says it's going to delete a specific table called posts. and the up function or the up method is actually creating a post table inside our database
and it has some information down here. As you can see right now, it has an ID and a timestamp. This, of course, is not enough for our posting system because this down here is the information that we need to change in order to make sure this is working properly.
Now, as you may know from my PHP course, if some of you have followed it, we, in some cases, went inside our PSP My Admin, we clicked on a database, went into SQL,
and we would then start creating a database table. We would say create table, call something, and then start filling in which columns to use for this particular table here, like an ID,
post title, post message, timestamp when created, so to speak. And that is actually what we're creating inside this method here. So right now it is creating a ID for us,
which is kind of neat, because that is something we need to do Now this one is automatically going to auto increment inside our table because that is just something we always do when we create a table Every entry inside that particular table is going to have an ID
And typically, always, we want to auto-increment that whenever we create a new entry inside that table. So, this one is automatically going to be auto-incremented. If I wanted to add something like, for example, a title, I could go below here and say I wanted to add a table property, and I'm going to point to something else, which in this case is going to be a string.
Now, this string here is going to be our, let's say, our title for this particular post. So we're going to call this one title. And I can copy this down and say I want to add, instead of a string, we can also add a text.
And we can call this one body. Just to see, like, this is the body, like the main message of this particular post here. You could also, if you wanted to, call it message, if that's something you wanted to do.
In my case, I'm just going to call it body. And what you're going to notice here is that whatever we point to is actually going to be the data type and we're going to insert inside our table. So in this case here, text is a form of long text format.
We do also have just a basic string up here. We have timestamps. Did we actually talk about this? I think we did. Like there was a previous episode we talked about like how to like also include restraints to this,
like a maximum amount of characters or something. But let's just create something very basic for now. So we're just going to create a ID. a title, a message, or a body in this case here, and a timestamp for when the post was created.
So with that change, what I can then do is I can actually go down inside my terminal, and I can migrate into my database again. It's basically pfp.y2send.migrate.
and that is going to migrate whatever migrations have not yet been migrated inside your migrations folder. So when I click it, it's going to update. You can see, oh, create post table.
If I go back inside, you can now see that when I refresh my first website, it now has a post table. If I were to click it, you can see that we have a couple of different columns.
We have an ID, we have created that, and we have a updated that, which actually should have some more stuff included. So now I forgot to save my file before migrating into the database, which does cause a couple of issues that can very easily be fixed if you know how to do it.
But I just want to, let's pretend I didn't forget to save. and then I'll show you how to actually fix it if you accidentally forgot to save and you need to do a rollback or something.
So right now, I saved the file. I just basically went in and deleted my post, my post controller, and deleted my Gration file. And then I just recreated them using the command we did in the last episode to create those model and post files.
That's quite a bit of a workaround, but that's just basically to reset. so I'm in the same place where you guys are. So I'm going to save this file. Then I'm going to migrate again.
It's messing up my terminal a little bit. I don't know why it's doing that. It's glitching, so to speak. So if I actually migrate, then it creates the post table. If I go inside my database, I refresh.
Then you can see I now have a post table that has title and body as being part of it. So now it actually works, right? Now, you will also notice that there's actually two separate columns for created at and updated at, which is actually automatically created when you use the timestamp feature down here.
So it does create two separate columns for that one particular line of code, just so you don't get confused. So a couple of things here. Let say I made a mistake and I need to roll back the latest migration I just did let say this is oh my god this is the wrong table you know we need to need to make changes to it before we re it
again. What I can do is I can write php artisan migrate colon rollback. If I do this, it is going
to roll back the latest change that I did inside my database table. So we go back in here and refresh. You can now see that my post table disappears. And if you want to re-migrate the files again, what you can do is you can go up and just say php artisan migrate once more. If I do that,
then you can see that it's running migrations. Oh, creating post table. And then it created it again inside my database. And we can actually double check this by going back in. And then You can see that at the post table. Now, in some cases, it may tell you that, well, there's no new migrations to run.
So in some cases, maybe you updated your file, and then you run the migrate command again, and then it just says, well, there's no new migrations because there's no new files inside the migration folder. How do you fix that?
Well, you can actually go in and target specific migration files and say to roll back a specific migration or to rerun a specific migration. Let's say I have maybe like 10 different migration files in here,
and I notice that, oh, okay, so it's not the latest migration. Like, let's say it's not this one, but this one up here. I need to rerun or roll back, you know, to like take it down or something,
like revert the change, so to speak. How do you do that? Well, if you go back down here, and you want to type in peer-to-peer artisan migrate, then you say colon rollback space dash dash path,
and then you simply link to the path that the file is inside of. So we're going to say path equal to, in this case here, it is going to be inside my databases folder or database folders,
so in this case database. then we're going to say forward slash migrations, if I know how to spell that correctly, migrations, forward slash, and then the name of the file. So in this case here, we could, you know, copy paste it.
We're just going to say rename, copy, go back down here, paste it in like so, dot PHP. And that is actually going to roll back a specific migration file.
So basically, you know, path, and then you point to that particular file you want to roll back. So if I click enter, it's going to roll back my post table, which is the same thing as just doing roll back because that was the latest file.
But in this case, we can also do it to an older file, not just the latest file that we migrated inside the migration folder. Now, if I want to rerun a particular file that isn't the latest file or all the files inside my project,
then I can do the exact same thing, but this time just not include rollback. So in this case, I'm going to go back and just kind of like, you know, delete rollback. So we just have it like this.
So migrate dash dash path. And then you're going to click enter. and then we recreate that particular migration just by simply trying to sing that particular migration also just so you know how to rerun all the
migrations inside your own migrations folder you can do PSP artisan migrate colon refresh and that is actually going to run all the migration files just
completely start over from scratch it's going to create the default migrations that we did when we installed RRL, all the new migrations you might have created, it's going to rerun all of them. So refresh is also a command you can use in order to just
completely start over if that's something you want to do. And that's simply the basic commands that I want to show you how to use And now there is actually one more thing I want to do before we end up this episode here which is actually very important in order for you to actually do anything when it comes to this table here at least when we start building our application If I were to go inside our model which we know is inside
our, if you go up here inside app, and then you go inside models, then you can see that post.php. Now, right now, if I were to try and actually create a post inside my database,
it is going to throw an error message because we need to actually manually tell our particular table or our particular post and controller system here for the post table to allow for something called math assignment.
Because when we actually fill in the title and the message for our post, and then we actually tell our application to create a new entry inside the table, inside our database, we need to actually allow for specific fields to be mass inserted inside the database.
And this is a security feature that is inside Laravel because in some cases users might update your table inside the website by going inside the developer tool and adding their own fields or changing something.
So we need to actually add a security step in order to tell our application which particular fields inside our posting system should be allowed to be passed as a mass assignment,
which is basically that we pass all the data as an array and run all the data and insert all the data inside our database table. So going inside our model, which right now was empty, which we talked about in the previous episode,
we do actually need to add in a line of code which looks like this. So basically this says that we're creating a protected, in this case here, a property called fillable,
and we're setting it equal to an array that has the values title and body. And this is basically going to say that, okay, so these particular fields or these particular columns inside our database
should be allowed to have a math assignment pass into the database table and be created, so to speak. Otherwise, when we do actually start creating our posting feature,
it's just going to say, oh, something wrong happened. You cannot math assign data to your database. So this is important to include in order to continue with the next episode.
and actually start creating a crud system. And just to mention it here, the reason that we only need to do the title and the body is because if I were to go inside my database here and go inside my post table,
ID is automatically getting created. So that one is not something we fill in when we create a new post. The title is something that we fill in and the body is something that we fill in. But created at and updated at
is also automatically getting created, not by us, but from our Laravel application. Like, it handles that automatically. So title and body are the only two that we, the user, type something into
in order to, you know, send something to the database. So that's why those two particular ones are the ones that we need to include inside the model in order to get this working. The other ones it doesn't care about because it's automatically getting filled in.
So with this said, now you know how to migrate data inside a database if you wanted to. You know how to create new tables. You know how to tell it which columns to include inside those tables.
And you also now know how to revert those changes. If you created a table, you want to revert back again because, oh no, that was incorrect. Then now you know how to change it and then rerun it again if you want to. So with that said, I hope you enjoyed this video and I'll see you guys in the next one.
Thank you.
