Adequate Infosoft
In this article, you will learn the step-by-step procedure to add single or multiple columns in our existing database tables using Laravel migration. In case you are struggling to >install Laravel then read our blog.
Step 1: To get started, open the command prompt or terminal and navigate to the root directory of our Laravel project.
Step 2: Type the following command to create a new migration file:
This code will create an add_type_to_notes file in the migration folder. it will look like this-
Now, replace the column_name with the name of the column you want to add and the table_name with the name of the table you want to modify.
Step 3: Open the newly created migration file in the database/migrations folder. Inside the up function, add the code to create the new column or columns:
Step 4: Run the migration command
Initially, our screen will look like the below image.
As soon as we run the migration command a new column will be added and our database will look like the below image.
To make our database look like the above image and add a new column, we have to add the below code to our database.
Adding Multiple Columns to an Existing Table using Laravel Migration
To add multiple columns to the existing table
Step 1: Navigate to the root directory and enter the following command
Step 2: As we want to add multiple columns to the existing table, we have to introduce the column name in our code.
Use the below command to introduce new column names.
Step 3: Adding code to the newly added migration file.
Step 4: Run the below command and check the database:
The final image shows that we have successfully added multiple columns in the existing table using Laravel migration.
Suggested blogs
>Make new Model/Controller/Migration in Laravel
>Laravel: Adding new column/columns to the existing table in a migration
>How to show encrypted user id in URL in Laravel?
>Run Laravel Project to Active Local Server
>How to upload images with filepond and vue 3?