Add column in existing table in Laravel using Migration

There are some times need to add new column to existing table in Laravel.There  are two way to do this .

One is go in your database and custom add here

Second way is using Laravel migration.It is most popular way to add new column.

PHP Artisan command to add new column to existing table in Laravel using Migration is as following.

php artisan make:migration add_image_to_categories_table --table=categories


When you run this command a migration file is created at database/migrations like '2020_05_26_091943_add_image_to_categories__table.php'




use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddImageToCategoriesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('categories', function (Blueprint $table) {
            $table----->string('image',391)->nullable();
            //
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('categories', function (Blueprint $table) {
            //
            $table->dropColumn('image');
        });
    }
}

The run following command

php artisan migrate


0 comments

There is no comments added for this topic
Post comment Comments (0)