Daily Web Designs

add-column-after-a-column-in-laravel-migration

Add Column After A Column In Laravel Migration

In this article, we will see how to add column after column in the Laravel migration. At some level of project completion, you might need to add the new column into the database table then you can easily add that by creating new migration in Laravel.

But you want to add that column after the particular column for better understanding. So let’s see how to do it.

Laravel Migration Add Column After

Laravel provides the after() method which used to place a new column after another column. Let’s create a new migration for users table and then we will add a new column into a newly created table for better understanding.
The above command will create a new migration in database/migrations directory. We have added the following code into the newly created migration.

Let’s now, we want to add the new 2 columns, middle_name after the first_name and the birth_date column after the password column. Let’s create a new migration to add these 2 columns.

In the above example, we have added 2 new columns in the up() method which will be executed when we run the php artisan migrate command. While we are dropping both columns in the down() method which will be executed when we run the php artisan migrate:rollback command.

Add Multiple Columns After A Column In Migrations

Laravel 9 has introduced a new after method which allows you to add multiple new columns after an existing column at the same time:

You can see the crystal clear difference of code clarity.
Leave a Reply

Shopping cart

0
image/svg+xml

No products in the cart.

Continue Shopping