How To Create And Run Laravel Migrations

Laravel migrations are a type of version control for your database. They allow a team to modify the database schema and stay up to date on the current schema state. Migrations are typically paired with the Schema Builder to easily manage your application’s scheme. In my last laravel post, I talked about laravel models. I also promised to show you how to create tables that you will need to store your blog posts(models) in your next blogging platform. Let us do this.

how to create laravel migrations

At this point, I assume that you already have a working database and you have configured it in your laravel application under app/config/database.php. Now we can move on.

How To Create And Run Laravel Migrations – Sample Code

Going a long with our Blogging platform idea from my previous posts; here is how we can create a migration for our posts table: open your terminal and change your directory to the application’s root folder: then :-

Now press Enter and be amazed by how easy it is to create laravel migrations. Once you have created a migration, you can open the newly created file in your favorite editor (shout out to Sublime Text 3). All laravel migrations have a way of naming that includes the time they were created. You can find your laravel migrations in the app/database/migrations folder.

Let us run our migration now

Issuing the above command on your terminal runs all the migrations that have not been ran in your application. It will create a table called posts in my database with the columns that I set in the migration up() function. It is that simple. You will notice that I added softDeletes so that when a blog post is deleted, it is not entirely eraced; instead, a deleted_at column is added to the posts table. Just in case the deletion was a mistake – in which case I can easily restore the post.

Rolling Back Laravel Migrations

Suppose you made a mistake or forgot to add an important column and yet you have already ran your laravel migrations, you can easily roll them back. This is an awesome undo feature you can use. Here is how you do that on your terminal:

The above command rolls back your last laravel migration. In other words, it executes the down() function in your migration file. You can also roll back all migrations you have ran since the beginning of your development. Here is how you do that:

Can it get any easier than that? Oh wait, there is even more:

The above command rolls back all your migrations and then immediately re-runs them for you while you enjoy your coffee or watch crazy cat videos on YouTube.

There is more to laravel migrations but what we have covered here today are the things you will use on a daily basis while developing your laravel application. If you want to find out more, you can always checkout the docs and I highly recommend bookmarking this cheat sheet here for reference.

Be The First To Get New Posts From Simple Developer…

Written By Elisha Chirchir

Elisha Chirchir is a software developer. He is also the founder of Simple Developer and co-founder of Instinctive Software Solutions. On any given day, he works on both Android and Web Development. During his 'free time', he offers training to those interested in learning how to code in php, java, python, javaScript etc. You can easily find him on StackOverflow Android chatroom or on Twitter @Eenvincible

Leave a Reply

Your email address will not be published. Required fields are marked *