Laravel 10|9|8 Migration Add New Column to Existing Table

Laravel 10|9|8 Migration Add New Column to Existing Table

Laravel add a column to existing table using migration example; In this tutorial, we will learn how to add a new columns to existing table using migration in laravel 10|9|8.

Sometimes there is a need to add new columns in your database tables. Today we will show you how to add new columns or columns to an existing table using Laravel migration.

Add New Column to Existing Table in Migration Laravel 10|9|8

Steps to add new column to existing table using migration in laravel 10|9|8 apps:

  • Add a single Column an existing table
  • Add Multiple Columns in Existing Table
  • Available Column Types

Let’s have a table called notes where the table you want to add notes type.

Add a single Column an existing table

To add a new column to the existing table using the laravel migration. Let’s open your terminal and create a migration file using the below command:

php artisan make:migration add_type_to_notes

It will create an add_type_to_notes file in the migration folder. It should be like this-

<?php

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

class AddTypeToNotes extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('notes', function (Blueprint $table) {
            $table->string('note_type')->nullable();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('notes', function (Blueprint $table) {
            $table->dropColumn(['note_type']);
        });
    }
}

Run Migration Command

Run the below-given command in your terminal. It will add a new column to your table.

php artisan migrate

Now you can go to your database table and to see there a new column in your table. It has been added successfully using the migration command:

Add Multiple Columns in Existing Table

If you need to add multiple columns, you need to add the following command in the terminal.

php artisan make:migration add_multiple_column_to_notes

The above command will create a migration file, so go to database/migration folder and open your file and add column according to you:

<?php

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

class AddMultipleColumnToNotes extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('notes', function (Blueprint $table) {
           $table->string('short_description')->nullable();
           $table->string('type')->nullable();
           $table->string('long_description')->nullable();

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('notes', function (Blueprint $table) {
           $table->dropColumn(['short_description',  'type', 'long_description']);
        });
    }
}

Finally, run the below-given command for adding columns in your database table:

php artisan migrate

Available Column Types

Laravel schema builder has provided a variety of column types that you may specify when building your tables:

CommandDescription
$table->bigIncrements('id');Auto-incrementing UNSIGNED BIGINT (primary key) equivalent column.
$table->bigInteger('votes');BIGINT equivalent column.
$table->binary('data');BLOB equivalent column.
$table->boolean('confirmed');BOOLEAN equivalent column.
$table->char('name', 100);CHAR equivalent column with an optional length.
$table->date('created_at');DATE equivalent column.
$table->dateTime('created_at');DATETIME equivalent column.
$table->dateTimeTz('created_at');DATETIME (with timezone) equivalent column.
$table->decimal('amount', 8, 2);DECIMAL equivalent column with a precision (total digits) and scale (decimal digits).
$table->double('amount', 8, 2);DOUBLE equivalent column with a precision (total digits) and scale (decimal digits).
$table->enum('level', ['easy', 'hard']);ENUM equivalent column.
$table->float('amount', 8, 2);FLOAT equivalent column with a precision (total digits) and scale (decimal digits).
$table->geometry('positions');GEOMETRY equivalent column.
$table->geometryCollection('positions');GEOMETRYCOLLECTION equivalent column.
$table->increments('id');Auto-incrementing UNSIGNED INTEGER (primary key) equivalent column.
$table->integer('votes');INTEGER equivalent column.
$table->ipAddress('visitor');IP address equivalent column.
$table->json('options');JSON equivalent column.
$table->jsonb('options');JSONB equivalent column.
$table->lineString('positions');LINESTRING equivalent column.
$table->longText('description');LONGTEXT equivalent column.
$table->macAddress('device');MAC address equivalent column.
$table->mediumIncrements('id');Auto-incrementing UNSIGNED MEDIUMINT (primary key) equivalent column.
$table->mediumInteger('votes');MEDIUMINT equivalent column.
$table->mediumText('description');MEDIUMTEXT equivalent column.
$table->morphs('taggable');Adds taggable_id UNSIGNED BIGINT and taggable_type VARCHAR equivalent columns.
$table->uuidMorphs('taggable');Adds taggable_id CHAR(36) and taggable_type VARCHAR(255) UUID equivalent columns.
$table->multiLineString('positions');MULTILINESTRING equivalent column.
$table->multiPoint('positions');MULTIPOINT equivalent column.
$table->multiPolygon('positions');MULTIPOLYGON equivalent column.
$table->nullableMorphs('taggable');Adds nullable versions of morphs() columns.
$table->nullableUuidMorphs('taggable');Adds nullable versions of uuidMorphs() columns.
$table->nullableTimestamps();Alias of timestamps() method.
$table->point('position');POINT equivalent column.
$table->polygon('positions');POLYGON equivalent column.
$table->rememberToken();Adds a nullable remember_token VARCHAR(100) equivalent column.
$table->set('flavors', ['strawberry', 'vanilla']);SET equivalent column.
$table->smallIncrements('id');Auto-incrementing UNSIGNED SMALLINT (primary key) equivalent column.
$table->smallInteger('votes');SMALLINT equivalent column.
$table->softDeletes();Adds a nullable deleted_at TIMESTAMP equivalent column for soft deletes.
$table->softDeletesTz();Adds a nullable deleted_at TIMESTAMP (with timezone) equivalent column for soft deletes.
$table->string('name', 100);VARCHAR equivalent column with a optional length.
$table->text('description');TEXT equivalent column.
$table->time('sunrise');TIME equivalent column.
$table->timeTz('sunrise');TIME (with timezone) equivalent column.
$table->timestamp('added_on');TIMESTAMP equivalent column.
$table->timestampTz('added_on');TIMESTAMP (with timezone) equivalent column.
$table->timestamps();Adds nullable created_at and updated_at TIMESTAMP equivalent columns.
$table->timestampsTz();Adds nullable created_at and updated_at TIMESTAMP (with timezone) equivalent columns.
$table->tinyIncrements('id');Auto-incrementing UNSIGNED TINYINT (primary key) equivalent column.
$table->tinyInteger('votes');TINYINT equivalent column.
$table->unsignedBigInteger('votes');UNSIGNED BIGINT equivalent column.
$table->unsignedDecimal('amount', 8, 2);UNSIGNED DECIMAL equivalent column with a precision (total digits) and scale (decimal digits).
$table->unsignedInteger('votes');UNSIGNED INTEGER equivalent column.
$table->unsignedMediumInteger('votes');UNSIGNED MEDIUMINT equivalent column.
$table->unsignedSmallInteger('votes');UNSIGNED SMALLINT equivalent column.
$table->unsignedTinyInteger('votes');UNSIGNED TINYINT equivalent column.
$table->uuid('id');UUID equivalent column.
$table->year('birth_year');YEAR equivalent column.

Conclusion

Laravel Migration example tutorial, in this tutorial you have learned how to add single or multiple columns in your database existing tables with example.

Recommended Laravel Tutorials

Recommended:-Laravel Try Catch

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

One reply to Laravel 10|9|8 Migration Add New Column to Existing Table

  1. very useful information.

Leave a Reply

Your email address will not be published. Required fields are marked *