Enhance Your Laravel Database: A Guide to ENUM Columns

 How to add enum column in table using migration in Laravel

Enhance-Your-Laravel-Database-A-Guide-to-ENUM-Columns

In this article we will learn How to add enum type column in your table using laravel migration . Laravel provides lots of column type to work with our database using migration one of them is enum . Enum type basically provides a predefine selection value in database that you will provide in your migration file as shown in the below image . Follow the below steps to create a enum type column in your table using migration in Laravel .

Add ENUM column in database using migration - Laravel


Step 1 - Crate migration :-

The first is to create a migration file , if you want to create a new table then you can create a migration file for creating new table in the database but in this example i will show to add a enum type column in an existing table in database . 

Use the following command to create a new migration file .

php artisan make:migration add_stock_status_column_to_items_table


Step 2 - Add following migration code :-

Use the following code to add enum type column in you table .

<?php

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

class AddStockStatusColumnToItemsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('items', function (Blueprint $table) {
            $table->enum('stock_status', ['in_stock', 'out_of_stock'])->default('in_stock');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('items', function (Blueprint $table) {
            //
        });
    }
}

Step 3 - Run migration command :-

Now simply use the following command to add column in your table .

php artisan migrate


Step 4 - Check your table :-

Now you can check your table it would have create column with the name you provided in your migration file .

Add ENUM column in database using migration - Laravel

Add more options in Enum column :

You can also add more options/values in your enum column using migration . Just create a new migration file and add the following code in it and then again run your migration command .

<?php

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

class AddOptionToItemsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('items', function (Blueprint $table) {
          DB::statement("ALTER TABLE items CHANGE COLUMN stock_status stock_status ENUM('in_stock', 'out_of_stock', 'few_items_left') NOT NULL DEFAULT 'in_stock'");
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('items', function (Blueprint $table) {
            //
        });
    }
}

In this above code , the following is the detail of the DB::statement code .

items - your table name

stock_status - column name

in_stock - option value

out_of_stock - option value

few_items_left - option value

Previous Post Next Post

Contact Form