Migrations work as a version control for databases. If you want to keep a record of your database creation and alterations over time, you can use migrations and simplify application re-deployment or distribution. Migrations will help you interact with your database by only using the CLI interface of Laravel. This means that after you install your application and configure the database connection, you just have to run “php artisan migrate” and it will instantly set up the database again.
For migrations, you need to have the following installed:
composer create-project laravel/laravel TestingMigrations
The default user and password will be “root” and empty, respectively.
Finally, after this, you’ll be able to see your Database Management System, where you can alter your databases already present or you’ll be able to see the tables created in the Laravel application.
*Execute the following command in the terminal:
php artisan make:migration create_users_table
If it gives you an error, delete the old migration and make this new one. Your new migrations will be created in the migration sub-directory of database directory of your application. It includes two functions:
Up function (responsible for making tables) Down function (responsible for dropping tables)
Add the following code in the up and down function of the created migration
Go the localhost and check if you can see your table.
Migrations commands along with their uses:
Migration command | Use |
---|---|
Php artisan make:migration |
Make migrations in your app |
Php artisan migrate |
Run your migrations |
Php artisan migrate --force |
Force running your migrations |
Php artisan migrate:rollback |
Rollback migrations |
Php artisan migrate:rollback --step=6 |
Rollback last 6 migrations |
If you want to keep on reading about Migrations in Laravel, you can try: