LARAVEL 9 MIGRATION - A COMPLETE GUIDE
Tags: laravel, webdev, beginners, tutorial
Published: 2022-06-07 20:46:17

Laravel migration is like version management tool of DATABASE.


![Laravel Migration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gtufbeukkkt4aq803mqj.png)

Let's Understand this better.

After [Installing Laravel](https://dev.to/shanisingh03/how-to-install-laravel-9-25c4) , let's understand migration.

## Create a migration and understand the structure

To generate a migration you need run a command

```php
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.
```php
public function up()
{
    Schema::create('contacts', function (Blueprint $table) 
    {
            $table->id();
            $table->string('name');
            $table->string('mobile_no');
            $table->boolean('status');
            $table->timestamps();
    });
}
```
whereas `down()` function holds information about reversing the migration action.

```php
public function down()
{
    Schema::dropIfExists('contacts');
}
```

## Run & Rollback The Migration

To run a migration we need to use the command

```php
php artisan migrate
```

For rolling back latest migration we have command 

```php
php artisan migrate:rollback
```

when we have to rollback till specific steps we can pass steps in rollback command like

```php
php artisan migrate:rollback --step=3
```

this will rollback the migration upto 3 step starting from latest.

## 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
php artisan make:migration update_name_column_in_contacts_table
```
Read more at [Laravel Doc](https://laravel.com/docs/9.x/migrations#columns)


## 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.

You can check the available [Laravel Column Modifier](https://laravel.com/docs/9.x/migrations#column-modifiers).


## Add/Rename/Remove Database Indexes

Laravel Migration support few types of INDEXES for e.g.

```php
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.
```php
$table->renameIndex('email', 'mobile_no');
```

For dropping a index you can use `dropIndex()` for e.g.
```php
$table->dropIndex('email');
```

## Foreign Key Constraints
Laravel also provides support for creating foreign key constraints, which are used to force referential integrity at the database level. 

```php
$table->foreignId('user_id')
      ->constrained('users')
      ->cascadeOnUpdate()
      ->cascadeOnDelete();
```

The Complete Video Tutorial is below in the video.

{% youtube EB2qi8o845k %}

If you face any issues while implementing, please comment your query.

Thank You for Reading

Reach Out To me.
[Twitter](https://twitter.com/shanisingh03)
[Instagram](https://www.instagram.com/shanisingh03/)
[YouTube](https://www.youtube.com/c/TechToolIndia)