Laravel Database Migrations


Laravel Database Migrations – The Laravel Database Migrations is used to controll on your database and allowing user to easily modify and create the database.


Laravel Database Migrations.

Let us understand how to use laravel Database Migrations.

Function:-

There are some followings function available in laravel Database Migrations.

  • 1. Introduction.
  • 2. Generating Migrations.
  • 3. Migration Structure.
  • 4. Running Migrations.
  • 5. Tables.
  • 6. Columns.
  • 7. Indexes.

1. Introduction.

It is like a version control for your database. Its allows user to easily modify and share the application database schema. Migrations are typically paired with Laravel’s schema builder to easily build your application’s database schema.

The Laravel schema facade provides database support for creating and manipulating tables across all of laravel’s supported database system.

2. Generating Migrations.

To create migration you can use make:migration Artisan Command.

php artisan make:migration create_users_table

The new migration will be placed at database/migrations directory

The –table and –create options can also be used to indicate the name of the table and the migration will be creating a new table.

php artisan make:migration create_users_table --create=users

php artisan make:migration add_votes_to_users_table --table=users

If you want to specify a custom output path for the generated migration, you can use the –path option when executing the make:migration command.

3. Migration Structure.

A migration class hold two method up and down. The up method is used to add new tables, columns or indexes to your database. The down method is used to delete tables and column from the database.

Let’s look at a simple example.

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateFlightsTable extends Migration
{
    public function up()
    {
        Schema::create('flights', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('airline');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('flights');
    }
}

4. Running Migrations.

To run all of your outstanding migrations, execute the migrate Artisan command.

php artisan migrate

Forcing Migrations To Run In Production

Some migration operations are destructive, which means they can cause you to lose data. In order to protect you from running these commands against your production database, you will be prompted for confirmation before the commands are executed. To force the commands to run without a prompt, use the –force flag.

php artisan migrate --force

Rolling Back Migrations

To rollback the latest migration operation, you can use the rollback command.

php artisan migrate:rollback

You can rollback a limited number of migrations by providing the step option to the rollback command.

php artisan migrate:rollback

Rolling Back Migrations

To rollback the latest migration operation, you can use the rollback command. This command rollback the last batch of migration, which is include multiple migration file.

php artisan migrate:rollback

You can rollback a limited number of migration by providing the step option to the rollback command.

php artisan migrate:rollback --step=5

The migrate:reset command will rollback all of your application migrations.

php artisan migrate:reset

Rollback & Migrate In Single Command

The migrate:refresh will rollback all of your migration and then execute the migrate command.

php artisan migrate:refresh

php artisan migrate:refresh --seed

The following command will rollback & re-migrate the last five migration.

php artisan migrate:refresh --step=5

5. Tables.

Creating Tables

To create a new database table, use the create method on the schema facade.
The create method accept two arguments. The first is the name of the table and the second is closure which receives a blueprint object that can be used to define the new table.

Schema::create('users', function (Blueprint $table) 
{
    $table->increments('id');
});

When creating the table, you can use any of the schema builders column methods to define the table columns.

Checking For Table / Column Existence

You can easily check for the existence of a table or column using the hasTable and hasColumn methods.

if (Schema::hasTable('users')) {
    //
}

if (Schema::hasColumn('users', 'email')) {
    //
}

Connection & Storage Engine

If you want to perform schema operation on a database connection that is not your default connection, you can use connection method.

Schema::connection('foo')->create('users', function (Blueprint $table) {
    $table->increments('id');
});

You can use the engine property on the schema builder to define the table’s storage engine.

Schema::create('users', function (Blueprint $table) {
    $table->engine = 'InnoDB';

    $table->increments('id');
});

Renaming / Dropping Tables

To rename an existing database table, use the rename method.

Schema::rename($from, $to);

To drop an existing table, you can use the drop or dropIfExists methods.

Schema::drop('users');

Schema::dropIfExists('users');

6. Columns.

Creating Columns

The table method on the schema facade can be used to update existing table

Schema::table('users', function (Blueprint $table) {
    $table->string('email');
});

Available Column Types

The schema builder contains a variety of column types that you can specify when buiding your tables.

Laravel Database Migrations

Column Modifiers

In addition to the column types listed above, there are several column “modifiers” you may use while adding a column to a database table.

Schema::table('users', function (Blueprint $table) {
    $table->string('email')->nullable();
});

Below is a list of all the available column modifier.

Laravel Database Migrations

Modifying Columns

Prerequisites

Before modifying a column, be sure to add the doctrine/dbal dependency to your composer.json file.

composer require doctrine/dbal

Updating Column Attributes

The change method allows you to modify some existing column types to a new type or modify the column’s attributes.

Schema::table('users', function (Blueprint $table) {
    $table->string('name', 50)->change();
});

We can also modify a column to be nullable.

Schema::table('users', function (Blueprint $table) {
    $table->string('name', 50)->nullable()->change();
});

Renaming Columns

To rename a column, you can use renameColumn method on the Schema builder.

Schema::table('users', function (Blueprint $table) {
    $table->renameColumn('from', 'to');
});

Dropping Columns

The dropColumn method is used to drop the column.

Schema::table('users', function (Blueprint $table) {
    $table->dropColumn('votes');
});

You can also drop multiple column from a table by passing an array.

Schema::table('users', function (Blueprint $table) {
    $table->dropColumn(['votes', 'avatar', 'location']);
});

7. Indexes.

Creating Indexes

The schema builder supports several types of indexes. For example that specifies a column’s values should be unique. To create the index, we can simply chain the unique method onto the column defination.

$table->string('email')->unique();

You can create the index after defining the column.

$table->unique('email');

You can pass an array of columns to an index method to create a compound index.

$table->index(['account_id', 'created_at']);
Laravel Database Migrations

Dropping Indexes

To drop an index, you must specify the index’s name. Laravel automatically assigns a reasonable name to the indexes. Simply concatenate the table name, the name of the indexed column, and the index type.

Laravel Database Migrations

If you pass an array of columns into a method that drops indexes, the conventional index name will be generated based on the table name, columns and key type.

Schema::table('geo', function (Blueprint $table) {
    $table->dropIndex(['state']); // Drops index 'geo_state_index'
});

Advertisements

Add Comment

đź“– Read More