Laravel Migration - A Complete Tutorial
Laravel Create Migration, Manage Columns, Creating Indexes, Managing Foreign Key Constraints.

Laravel | VueJs | PHP | Shopify | ReactJs | MySql
Search for a command to run...
Laravel Create Migration, Manage Columns, Creating Indexes, Managing Foreign Key Constraints.

Laravel | VueJs | PHP | Shopify | ReactJs | MySql
No comments yet. Be the first to comment.
Eloquent Factory & DB Seeder

Log in without a password by using the magic link in Laravel.

Verify Registered Mobile Number

Login with username / Mobile No / UUID

Upload image & Store in Public, Store or AWS S3.

Laravel migration is like a version management tool of DATABASE.

Let's understand this better.
After Installing Laravel, let's understand migration.
To generate a migration you need to run a command
php artisan make:migration create_contacts_table
this will generate a file in database\migrations folder.
The file consists of a 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.
public function up()
{
Schema::create('contacts', function (Blueprint $table)
{
$table->id();
$table->string('name');
$table->string('mobile_no');
$table->boolean('status');
$table->timestamps();
});
}
whereas the down() function holds information about reversing the migration action.
public function down()
{
Schema::dropIfExists('contacts');
}
To run a migration we need to use the command
php artisan migrate
For rolling back the latest migration we have a command
php artisan migrate:rollback
when we have to roll back to specific steps we can pass steps in the rollback command like
php artisan migrate:rollback --step=3
this will roll back the migration up to 3 steps starting from the latest.
We need to generate a migration file similar to what we have created while creating the migration to perform any task.
the only change will be there in the 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 commands like
php artisan make:migration update_name_column_in_contacts_table
Read more at Laravel Doc
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.
You can check the available Laravel Column Modifier.
Laravel Migration supports a few types of INDEXES for e.g.
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Schema::table('users', function (Blueprint $table) {
$table->index('email');
});
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');
Laravel also provides support for creating foreign key constraints, which are used to force referential integrity at the database level.
$table->foreignId('user_id')
->constrained('users')
->cascadeOnUpdate()
->cascadeOnDelete();
The Complete Video Tutorial is below in the video.
If you face any issues while implementing, please comment your query.
Thank You for Reading