How to Add New Table Fields in Laravel

In laravel has models control data tables and migrations to create and manage models. As we use migrations to create tables, we have to use migrations to update or add new fields in a data table.

Add New Table Fields in Laravel

Here we will add new columns in a table by following three easy steps.

Step 1: Make Migration

To update any data table first, we have to create a migration for that table.

Go to your laravel project’s root directory and write the below code to make migration to the table.

php artisan make:migration migration_name --table=table_name

Note: Change migration_name and table_name according to your project requirement.

Step 2: Edit Migration File to Add New Fields

First, open the newly created migration file to add new columns to the table. Follow the below path to reach the migration directory from your project root directory.

cd database/migrations/

After reaching there, open the newly created migration file. As you can see, there will be up() and down() predefined methods. Let’s see what these methods do in brief.

Laravel Migration up() Method

up() method works when we run the migration. In the up() method, we have to add table fields which we need to add/edit.

In our case, we will add some new columns to the data table.

public function up()
    Schema::table('table_name', function (Blueprint $table) {
        //add field details here

Laravel Migration down() Method

down() method works when we roll back the migration. In the down() method, we have to define what changes are needed in the data table if the migration is rollbacked.

In our case, we will remove newly added columns from the data table.

public function down()
    Schema::table('table_name', function (Blueprint $table) {
        //remove columns from data table

Step 3: Run Migration

We are almost ready to migrate the data table now. Just run the below command at the root directory of your laravel project.

php artisan migrate

And it’s done πŸ™‚


I hope now you have a complete understanding of laravel data table migration and how to add new columns in the table.

Have a good day, Artisan πŸ™‚

Leave a Reply

Your email address will not be published. Required fields are marked *