Laravel 8 Tutorial - Migration

In this tutorial, we will learn about database migrations in Laravel.

Migrations are like version control for your database, allowing your team to easily modify and share the application's database schema.

Migrations are typically paired with Laravel's schema builder to easily build your application's database schema.

Let's create a migration. Before creating a migration, go to phpMyAdmin and you will see that there are no tables in the database.

Now, let's create a migration for the posts table. Go to the command prompt and run the following command:


php artisan make:migration create_posts_table --create=posts

Here, "posts" is the table name. The migration has been created inside the migration folder.

Let's find this migration. Go to the project and click on the database folder, then click on migrations. You will see the "create_post_table" migration.

Migrations contain a class definition with up() and down() methods. The up() method is run when the migration is executed to apply changes to the database, and the down() method is run to revert the changes.

If you need to update the database, you just create a new migration.

Now, inside the up() function, you will see the id column, which is auto-incrementing and of type bigInt, and the timestamp, which creates two columns: created_at and updated_at.

Let's add some more columns:


$table->string('title');
$table->text('body');

Now, save the file and let's migrate this migration. Switch to the command prompt and run the following command:


php artisan migrate

Alright, now the migration has been migrated. Let's check the database. Go to phpMyAdmin and refresh the database. You will see that the tables have been created.

These four tables are Laravel's default migration tables. The migration table takes care of all the migrations, and this is the posts table. See inside the posts table all the columns that were added inside the posts migration file. And these created_at and updated_at columns are coming from the timestamp.

Now, let's see the rolling back migrations. To rollback the latest migration operation, we can use the rollback command. So, just go to the command prompt and type:


php artisn migrate:rollback

Now, go to phpMyAdmin and refresh the database. You will see that only the migration table is here, and the other tables have been removed because of the rollback migration command.

Alright, now let's see the refresh migrations. The migrate:refresh command will roll back all of your migrations and then execute the migrate command. This command effectively re-creates your entire database.

So, go to the command prompt and run the command:


php artisan migrate:refresh

It's executed. Now, you can see that all tables have been re-created.

Let's do one thing: insert some records into the posts table. Open the posts table and click on insert. Insert one or two records. So, just type here:

Title and body. Once again, title and body. Now, click on go. Click on browse. You will see the inserted records.

Now, execute the migrate:refresh command. So, go to the command prompt and type:


php artisan migrate:refresh

Ok, the command has been executed. Now, go to phpMyAdmin. You will see that all tables have been re-created, and inside the posts table, there are no records.

So, in this way, you can use migrations in Laravel 8.