How To Add Column In Existing Table In Laravel 10

Websolutionstuff | Apr-24-2023 | Categories : Laravel MySQL

In this article, we will see how to add a column to the existing table in laravel 10 migration. Here, we will learn about adding multiple columns in existing laravel 10 migration. The table method on the Schema facade may be used to update existing tables.

So, let's see how to add columns in an existing table using migration in laravel 10, laravel 10 migration add a column to an existing table, add multiple columns in laravel migration, and how to add a column after a specific column in laravel migration.

Like the create method, the table method accepts two arguments: the name of the table and a closure that receives an Illuminate\Database\Schema\Blueprint instance you may use to add columns to the table

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
 
Schema::table('users', function (Blueprint $table) {
    $table->integer('votes');
});

 

Create Migration:

 Create migration using the following command.

php artisan make:migration create_flights_table

Migration:

<?php
 
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
 
return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('flights', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('airline');
            $table->timestamps();
        });
    }
 
    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::drop('flights');
    }
};

 

Add Column using Migration:

<?php
 
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
 
return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('flights', function (Blueprint $table) {            
            $table->string('passenger_name');            
        });
    }
 
    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        $table->dropColumn('passenger_name');
    }
};

 

Add Column After another column using Migration:

When using the MySQL database, the after method may be used to add columns after an existing column in the schema.

<?php
 
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
 
return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('flights', function (Blueprint $table) {            
            $table->string('passenger_name')->after('airline');            
        });
    }
 
    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        $table->dropColumn('passenger_name');
    }
};

 


You might also like:

Recommended Post
Featured Post
Laravel Rollback Targeted Migration Reversals
Laravel Rollback Targeted Migr...

As a Laravel developer, I understand the significance of migrations in managing database changes and maintaining a consi...

Read More

May-29-2023

How To Get env Variable In Controller Or Blade File
How To Get env Variable In Con...

In this tutorial, I will give you information on how to get the .env variable in the controller or file. Many times we n...

Read More

Jul-26-2020

Laravel 9 Resize Image Before Upload
Laravel 9 Resize Image Before...

In this article, we will see how to resize image before uploading in laravel 9. we will install the intervention/im...

Read More

Jun-15-2022

How To Install Vue 3 In Laravel 9 With Vite
How To Install Vue 3 In Larave...

In this article, we will see how to install Vue 3 in laravel 9 with vite. In the previous article, we will install...

Read More

Oct-10-2022