Migrating Commands

Updated on Jul 11, 2024

Earlier in this tutorial, we mentioned that migrations can insert tables directly into your application's database without actually opening it. Such migrations can be done via the Artisan CLI with a few commands. In this part, we will lay them out and explain what each does. In the previous section, we mentioned another command: php artisan make:migration. That is the fundamental command when it comes to inserting new tables into your database. After the command, you must specify a name for the table, and if it is more than one word, each word has to be separated from the rest via an underscore, with no empty spaces. Here is an example.

php artisan make:migration fastcomet_table_one

Once the creation process is completed, you will see a confirmation message. The new migration will be placed in your application's database/migrations directory, and each file within the directory has a timestamp. This allows Laravel to determine the order of creation. 

With that said, here are the remainder of the migration-related commands that you can use to manage your database's table.

php artisan migrate:fresh - Drops all tables and re-runs all migrations, effectively resetting your database and then executing the migrate command;

php artisan migrate:install - Creates the migration repository, which is typically not created by default;

  • php artisan migrate:refresh - Resets and re-runs all migrations. This is different from the first command, as it does not wipe the database and will also run all migrations you had done up until that point;
  • php artisan migrate:reset - Rollbacks all database migrations. The command can be used to revert all of your application's migrations to a previous state;
  • php artisan migrate:rollback - Rollback the last database migration. You can use this command to revert your database to the state it was in before the latest migration batch;
  • php artisan migrate:status - Show the status of each migration. Append the migration name you want to check and run this command for detailed information.

Migrations are especially useful when a whole team is working on an application. That way, they don't have to pass the database's actual file and import it every time they have made a change. With migrations, changes to the database can be made and reverted on the spot.

On this page...