How to Add Different Column in Multiple Tables in Laravel ?

10-Mar-2023

.

Admin

Hi Dev,

Today, I am going to teach how to create one migration in add multiple table using schema in laravel application. We will show how to insert column in multiple table but only one create migration in laravel using schema.

In this tutorial, I will show you create one migration file add column in multiple table using schema in laravel.

Here i will add three tables like users table in type column, categories table in user_id column and blogs table in description column. So let's see bellow example.

Example


Run bellow command

php artisan make:migration add_columns

database/migrations/2020_09_07_155910_add_columns.php

<?php

use Illuminate\Database\Migrations\Migration;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Support\Facades\Schema;

class AddColumns extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

Schema::table('users', function(Blueprint $table) {

$table->tinyInteger('type')->default(0);

});

Schema::table('categories', function(Blueprint $table) {

$table->integer('user_id')->nullable();

});

Schema::table('blogs', function(Blueprint $table) {

$table->string('description')->nullable();

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::table('users', function (Blueprint $table) {

$table->dropColumn('type');

});

Schema::table('categories', function (Blueprint $table) {

$table->dropColumn('user_id');

});

Schema::table('blogs', function (Blueprint $table) {

$table->dropColumn('description');

});

}

}

It will help you...

#Laravel 7

#Laravel

#Laravel 6