In laravel while creating table schema for some columns we want to add column type enum
field so in this article we will create Enum Field in Laravel Migration. We can easily add enum
column as we add other varchar
integer etc columns in laravel. Enum is useful when we want to store specific predefined string in the column only and it restrict to store any type of data.
We will cover in this example with any version of laravel 5, laravel 6, laravel 7, laravel 8 and laravel 9.
In laravel migration we can easily create enum
field using enum
method of laravel Schema
class and it accepts two parameters first as name of column and second enum values in array.
Here is the syntax
$table->enum('status',['1','0']);
//we can also set default value
$table->enum('status',['1','0'])-> default('0');
We will use the php
artisan command to generate the Create Enum Field in Laravel migration and php artisan migrate
command to modify the table.
Let’s understand the Create Enum Field in Laravel Migration in laravel step by step
Step 1 : Generate migration file
To generate the migration file we will use the laravel artisan command so open the terminal in project and run below command
php artisan make:migration movies_table
Above command will create a migration file in folder database/migrations
Output: Created Migration: 2022_06_30_174050_movies_table
Step 2 : Open generated migration file and update
In the last step we created a migration file using the artisan command and now we wanted to add enum column to the table so adding a status
column as enum .
so let’s open the file and start editing
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateMoviesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('movies', function (Blueprint $table) {
$table->id();
$table->string('email')->unique();
$table->int('user_id');
$table->string('title');
$table->string('body')->nullable();
$table->enum('status',['NEW','OLD']);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('movies');
}
}
as you can see we have created enum field using $table->enum('status',['NEW','OLD'])
syntax.
Method 2 : Enum field with Default Value
In this method we will use enum
values with default value
Schema::create('movies', function (Blueprint $table) {
$table->id();
$table->string('email')->unique();
$table->int('user_id');
$table->string('title');
$table->string('body')->nullable();
$table->enum('status',['NEW','OLD'])->default("OLD");
$table->timestamps();
});
Step 3 : Run Migration
In this step we will execute our migration in database using below command
php artisan migrate
This will create table in database and the output
Output: Migrating: 2022_02_15_174050_movies_table Migrated: 2022_02_15_174050_movies_table (20.08ms)
Also Read : How to Update Enum Field Value using Laravel Migration ?