Adding or Removing a Column from an Existing Database Using Laravel Migration
Migrations are a type of version control system because It controls your database using migration file. Have you used a version control system before? Actually It’s essentially a way of moving from one configuration, to another, or back to the previous version of how a database schema was. we need this system for adding or removing existing database migration in Laravel.
Scenario :
Suppose you are maintaining your database version. In version 1.1 you have the following table structure with lots of data.
USER & CUSTOMER TABLE
id | name | created_at | updated_at | FOR USER TABLE |
id | name | created_at | updated_at | FOR CUSTOMER TABLE |
After a certain time, you need to add an extra column when you have lots of existing data. In this case, if you edit your existing migration file then run artisan command with rollback then all data will be lost from your database. Then what will you do now?
Most importantly the best way to use laravel migration. There are up() and down() functions in laravel migration system. You can create your table in using up() function and you can rollback your table using down() function.
A laravel migration command to generate a migration file for creating a customer_profiles table. This migration file is very important for Laravel migration for an existing database.
Command :
1 2 3 4 |
php artisan make:migration createCustomerProfilesTable |
This command will generate a bellow file in the app/database/migrations directory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateCustomerProfilesTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('customer_profiles', function (Blueprint $table) { $table->increments('id'); $table->string('name')->nullable(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('customer_profiles'); } } |
In this up() and down() function define that create a customer_profiles table with id, name, created_at and updated_at field and down() function represent that if table customer_profiles has existed in your database then drop it.
But my question is if you need to modify or remove the column of any table then what will look like that migration file?
Therefore, A simple migration command is available for doing any kind of customization in the existing table.
Command :
1 2 3 4 |
php artisan make:migration create_customer_address_to_customer_profiles_table --table=customer_profiles |
–table flag means the table name where you want to modify anything you want.
this will generate bellow code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateCustomerAddressToCustomerProfilesTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::table('customer_profiles', function (Blueprint $table) { // }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('customer_profiles', function (Blueprint $table) { // }); } } |
In the up() function you need to add your desire field that you want to add and in the down() function you can drop column name that you want. when you will run artisan:rollback command that everything will remain as like previous.
After adding the address field migration file looks like this.
Command :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateCustomerAddressToCustomerProfilesTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::table('customer_profiles', function (Blueprint $table) { $table->string('address')->nullable(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('customer_profiles', function (Blueprint $table) { $table->dropColumn('address'); }); } } |
Now if you migrate this migration file, it will add an address field to your existing table. Moreover, when you are running a rollback command, as a result, the address column will be removed from the customer_profile table.
That’s it, guys.
Feel free to make comment if you have anything need to know.
1 Response
[…] this article, I am going to discuss the MySql Join Query. Today we will learn about MySql Join Query and why they are using it and how to use […]