How to Create & Manage Migrations in Laravel 9
I’m going to show you how to create migration in laravel 9. We will implement a how to create table migration in laravel 9.I will guide you how to create database table using laravel migration.
To generate a migration you need run a command
php artisan make:migration create_contacts_table
this will generate a file in database\migrations folder.
The file consist of new class extending the migration class of LARAVEL.
The new class consist of 2 major function up() & down(). The up() function holds all information about migrating the file.
The down() function holds information about reversing the migration action.
Run & Rollback The Migration
To run a migration we need to use the command
php artisan migrate
For rolling back latest migration we have command
php artisan migrate:rollback
when we have to rollback till specific steps we can pass steps in rollback command like
php artisan migrate:rollback --step=3
## Adding/Updating Columns in Table
To perform any task we need to generate a migration file similar to what we have created while creating the migration.
the only change will be there in migration name, always try to write the migration name descriptive which helps laravel to understand the table name in migrations. For e.g. Updating the column name we should run command like
php artisan make:migration update_name_column_in_contacts_table
Column Modifiers
Column Modifiers are nothing but a predefined function available in LARAVEL Migration by using that you can make any column nullable, Set Column default and many more.
Add/Rename/Remove Database Indexes
Laravel Migration support few types of INDEXES for e.g.
For renaming a index you can use renameIndex() for e.g.
$table->renameIndex('email', 'mobile_no');
For dropping a index you can use dropIndex() for e.g.
$table->dropIndex('email');
Drop All Tables & Migrate
The migrate:fresh command will drop all tables from the database and then execute the migrate command:
Renaming / Dropping Tables
To rename an existing database table, use the rename method:
To drop an existing table, you may use the drop or dropIfExists methods:
Using this tutorial we will learn how to create migration in laravel 9. We will implement how to create table able using migration in laravel 9.We learn how to run migrations.