Inside this article we will see the concept i.e Laravel 9 How To Add Enumeration Column in Migration. Article contains the classified information about adding a enum data type in laravel database via migration.
Laravel by default provides several methods in migrations for different different types of data types. If you are looking for an article which gives you the understanding of adding enumeration column in database table then this article gives you helps you a lot.
Learn More –
- Laravel 9 Cookies – Get, Set, Delete Cookie Example Tutorial
- Laravel 9 Ajax DELETE Request Example Tutorial
- Laravel 9 Custom Blade Component Example Tutorial
- Laravel 9 How to Get Request Parameter Tutorial
Let’s get started.
Laravel Installation
Open terminal and run this command to create a laravel project.
composer create-project laravel/laravel myblog
It will create a project folder with name myblog inside your local system.
To start the development server of laravel –
php artisan serve
URL: http://127.0.0.1:8000
Assuming laravel already installed inside your system.
Create Database & Connect
To create a database, either we can create via Manual tool of PhpMyadmin or by means of a mysql command.
CREATE DATABASE laravel_app;
To connect database with application, Open .env file from application root. Search for DB_ and update your details.
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel_app DB_USERNAME=root DB_PASSWORD=root
Example #1 (Add Enum Column)
Add Enum Data Type Column.
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('blogs', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('body'); $table->enum('status', ['Pending','Active','Inactive']); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('blogs'); } };
Concept
This will add status column with these given values.
$table->enum('status', ['Pending','Active','Inactive']);
Example #2 (Add Enum Column with Default value)
Add Enum Column with Default Value.
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('blogs', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('body'); $table->enum('status', ['Pending','Active','Inactive'])->default('Pending'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('blogs'); } };
Concept
This will add enum data type with a default value.
$table->enum('status', ['Pending','Active','Inactive'])->default('Pending');
Output
We hope this article helped you to Laravel 9 How To Add Enumeration Column in Migration Tutorial in a very detailed way.
Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.