Laravel Database Migration

Laravel Database Migration

Instructor-svgAl-Mamun Sarkar
Jun 22 , 2020
  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram

এই লেসনে লারাভেল Database Migration নিয়ে বিস্তারিত আলোচনা করবো। ডাটাবেস টেবিল তৈরি করা, আপডেট করা। Migrate করা Migrate Rollback করা ও Migration সম্পর্কে বিভিন্ন বিষয় নিয়ে বিস্তারিত আলোচনা করবো। 

 

Artisan Commands:

Create Migrarion:

php artisan make:migration create_customers_table
php artisan make:migration create_customers_table --create=customers
php artisan make:migration add_votes_to_customers_table --table=customers

 

Run migration:

php artisan migrate

Run with force:

php artisan migrate --force

Rolling Back:

php artisan migrate:rollback

Rolling Back With Step:

php artisan migrate:rollback --step=3

Rolling Back all migration:

php artisan migrate:reset

Rolling Back and Migration:

php artisan migrate:refresh

Drop All Tables & Migrate:

php artisan migrate:fresh

 

Installing Package:

composer require doctrine/dbal

 

 

Source Code:

CreateCustomersTable:

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCustomersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('customers', function (Blueprint $table) {
            $table->id();
            $table->string('name', 100);
            $table->string('email');
            $table->string('address');
            $table->double('balance');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('customers');
    }
}

 

CreateEmployeesTable:

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateEmployeesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('employees', function (Blueprint $table) {
            $table->id();
            $table->string('name', 100);
            $table->string('email', 100);
            $table->double('salary');
            $table->date('dob');
            $table->integer('family_members');
            $table->text('description');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('employees');
    }
}

 

CreateSalaryTable:

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateSalaryTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('salary', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('salary');
    }
}

 

DeleteCustomersTable:

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class DeleteCustomersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::dropIfExists('customers');
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::create('customers', function (Blueprint $table) {
            $table->id();
            $table->string('name', 100);
            $table->string('email');
            $table->string('address');
            $table->double('balance');
            $table->timestamps();
        });
    }
}

 

CreateSalesTable:

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateSalesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('sales', function (Blueprint $table) {
            $table->id();
            $table->double('amount');
            $table->string('reason')->comment('This is reason column');
            $table->string('description')->nullable();
            $table->boolean('status')->default(true);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('sales');
    }
}

 

AddDesignationToEmployeesTable:

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddDesignationToEmployeesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('employees', function (Blueprint $table) {
            $table->string('designation')->nullable()->after('email');
            $table->integer('family_members')->nullable()->change();
            $table->string('email', 70)->change();
            $table->renameColumn('name', 'username');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('employees', function (Blueprint $table) {
            $table->dropColumn('designation');
            $table->integer('family_members')->change();
            $table->string('email', 100)->change();
            $table->renameColumn('username', 'name');
        });
    }
}

 

CreatePostsTable:

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id');
            $table->string('title');
            $table->text('description');
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
}