DEV Community

loading...
Cover image for How to add a new column in Laravel table using migration

How to add a new column in Laravel table using migration

Osman Forhad
👨‍💻Mobile & Web Application Developer (ReactNative, Nodejs, MongoDB, React, Laravel, Mysql)
・1 min read

I was working with Laravel framework and i create a laravel migration table and working with it but after some times i need to add another column on my table and its should make using laravel migration. for that what was the process i deed. i want to share may knowledge with you.

for that the very first step was. i opened my terminal and navigate to project directory after that i was run below command on my terminal:

php artisan make:migration add_category_status_to_categories_table --table=categories

this command create a migration file for me and then i open this file and wrote my migration schema for add new column into my database table. when i open the fle i saw there two function one is public function up and another is public function down.

To add a new column i updated those functions which is like bellow:
//the up function is
public function up()
{
Schema::table('categories', function (Blueprint $table) {
$table->enum('category_status', ['active', 'deactive'])->after('category_slug')->default('active');
});
}
//the down function is
public function down()
{
Schema::table('categories', function (Blueprint $table) {
$table->enum('category_status', ['active', 'deactive'])->after('category_slug')->default('active');
});
}

all are set now i am ready to migrate my new column schema for that i go to my terminal and run below command:

php artisan migrate

and all are done now i seen the category_status column is added after category_slug column on my categories table.

that's it.
.
Happy Coding.
osman forhad

Discussion (0)