DEV Community

Cover image for How to add a new column in Laravel table using migration
Osman Forhad
Osman Forhad

Posted on

How to add a new column in Laravel table using migration

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

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (1)

Collapse
 
devt204 profile image
Dev Tyagi

fle,deed please fix the typos

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay