• 5 min read
All Migration Commands And Options in Laravel Explained
When working with Laravel, the php artisan migrate
command is a crucial tool for managing your database migrations. It allows you to create and update database tables with ease. However, there are many different commands and options that can be used with this command, and it can be confusing to know which one to use in which situation. In this blog post, we will discuss all the available php artisan migrate
commands and options, and when to use each of them.
The migrate
Command
The migrate
command is the most commonly used command and is used to run any new database migrations that have been created. This command will run all of the migrations that have not yet been run. It is important to note that the migrate
command will not run any migrations that have already been run in the past. To use this command, simply run php artisan migrate
.
The migrate:fresh
Command
The migrate:fresh
command is used to drop all tables from the database and then run all migrations again from scratch. This is useful when you need to start over with a fresh database. It is important to note that this command will delete all of your data, so be careful when using it. To use this command, run php artisan migrate:fresh
.
The migrate:install
Command
The migrate:install
command is used to create the migrations table in the database. This table is used to keep track of which migrations have been run. You only need to run this command once when setting up your project. To use this command, run php artisan migrate:install
.
The migrate:rollback
Command
The migrate:rollback
command is used to undo the last migration that was run. This command can be used to revert a migration that was run by mistake. It is important to note that this command will only undo the last migration that was run. To use this command, run php artisan migrate:rollback
.
The migrate:status
Command
The migrate:status
command is used to display the status of all migrations. This command shows which migrations have been run and which migrations still need to be run. To use this command, run php artisan migrate:status
.
The migrate --pretend
Option
The migrate --pretend
option is used to show which migrations will be run without actually running them. This can be useful if you want to see what will happen before actually making any changes to your database. To use this option, run php artisan migrate --pretend
.
The migrate --database
Option
The --database
option allows you to specify which database connection to use when running migrations. This is useful when you have multiple database connections in your Laravel project. To use this option, run php artisan migrate --database=name_of_database
.
The migrate --force
Option
The migrate --force
option is used to run all outstanding database migrations without prompting for confirmation. This can be useful for running migrations in automated scripts or for quickly updating a development database. It is important to note that this command should be used with caution, as it can overwrite existing data in your database. To use this option, run php artisan migrate --force
.
The migrate --path
Option
The php artisan migrate --path
option is used to specify a path to a directory containing migration files. This is useful for running migrations that are not in the default database/migrations
directory. To use this option, run php artisan migrate --path=path_to_directory
.
The migrate --realpath
Option
The php artisan migrate --realpath
option is used to resolve migration paths relative to the project’s root directory. This can be useful when working with a non-standard project structure. To use this option, run php artisan migrate --realpath
.
The migrate --schema-path
Option
There is no php artisan migrate --schema-path
option available. However, you can use the migrate --path
option to specify a path to a directory containing migration files. This can be useful for running migrations that are not in the default database/migrations
directory. To use this option, run php artisan migrate --path=path_to_directory
.
The migrate --seed
Option
The php artisan migrate --seed
command is used to run all of your database migrations and then seed your database with data. This is useful when you want to quickly populate your database with test data. To use this command, you must have created seed classes. You can create seed classes using the make:seeder
command. Once you have created your seed classes, you can run the php artisan migrate --seed
command to run all of your migrations and seed your database.
The migrate --step
Option
The php artisan migrate --step
option is used to run a limited number of migrations. By default, all outstanding migrations are run. However, you can limit the number of migrations that are run by specifying a number after the --step
option. This can be useful if you want to run migrations in batches or if you want to limit the amount of time that the migration process takes. To use this option, run php artisan migrate --step=number_of_migrations_to_run
.
Conclusion
The php artisan migrate
command is a powerful tool for managing your database migrations in Laravel. Using the right command and options for your situation can save you a lot of time and effort. We have discussed all of the available commands and options, and when to use each of them. By understanding these commands, you can confidently manage your database migrations in Laravel.
I hope this article has shown you all possible migrate options and flags. If you enjoyed reading this post and would like me to write an article on a specific topic, please send an email to info@codewithdary.com.