How to add a new column to an existing table of Laravel in a migration?


The make:migration Artisan in Laravel (9) enables you to generate a database migration. You can find the resultant migration files are the database/migrations directory. Using this command, we can add a new column to a table (in addition to other operations).

Syntax

First of all let us first create a table using the make:migration command. Following is the syntax to create a new table in Laravel using this command −

php artisan make:migration create_yourtablename_table

Here, the table name is your_table_name. So let us create a table with the name: students.

The command to create table students is as follows

php artisan make:migration create_students_table
C:\xampp\htdocs\laraveltest>php artisan make:migration create_students_table
Created Migration: 2022_05_01_055549_create_students_table

C:\xampp\htdocs\laraveltest>

Now, if you observe inside database/migrations you can observe an auto-generated class created for students as shown below –

<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateStudentsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('students', function (Blueprint $table) { $table->id(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('students'); } }

The class contains two methods up() and down(). The up() takes care of holding the schema for the table named students. By default, the id and timestamps are created, and you can add more fields as per the requirement.

Let us add the name and email fields to the up() method as shown below −

public function up() {
   Schema::create('students', function (Blueprint $table) {
      $table->id();
      $table->string('name');
      $table->string('email');
      $table->timestamps();
   });
}

Now, to create the table:students inside the database, we need to run the following command −

php artisan migrate
C:\xampp\htdocs\laraveltest>php artisan migrate
Migrating: 2022_05_01_055549_create_students_table
Migrated: 2022_05_01_055549_create_students_table (21.14ms)
C:\xampp\htdocs\laraveltest>

After the success of the command, you should be able to see the table created in MySQL as −

Adding a New column to the Existing Table

Now, that the table namedstudents” is created, let us add a column to this table. Here we are adding a new column named address. The command to do so will be as follows −

php artisan make:migration add_address_to_students --table="students" C:\xampp\htdocs\laraveltest>php artisan make:migration add_address_to_students --table="students" Created Migration: 2022_05_01_061430_add_address_to_students C:\xampp\htdocs\laraveltest>

Now, if you check the file inside database/migrations. The content of the auto generated file is as follows

<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class AddAddressToStudents extends Migration { /** * Run the migrations. * @return void */ public function up() { Schema::table('students', function (Blueprint $table) { }); } /** * Reverse the migrations. * @return void */ public function down() { Schema::table('students', function (Blueprint $table) { // }); } }

Inside the up() method you can add your new column as shown below −

public function up() {
   Schema::table('students', function (Blueprint $table) {
      $table->text('address');
   });
}

There is also a down() method that takes care of dropping the newly created columns. So add the address column to be dropped inside the down() method. This is useful if the migration command is executed again.

public function down() {
   Schema::table('students', function (Blueprint $table) {
      $table->dropColumn('address');
   });
}

Now, let us run the PHP artisan migrate to make the change on the table:students. The command is as follows −

php artisan migrate
C:\xampp\htdocs\laraveltest>php artisan migrate
Migrating: 2022_05_01_061430_add_address_to_students
Migrated: 2022_05_01_061430_add_address_to_students (21.51ms)

C:\xampp\htdocs\laraveltest>

Now take a look at the table: students inside the database, you should see the newly added column as shown below −

Updated on: 29-Aug-2022

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements