DEV Community

Cover image for Update Table in Laravel Migrations
Chetan Rohilla
Chetan Rohilla

Posted on • Updated on • Originally published at w3courses.org

Update Table in Laravel Migrations

Here, i have come with another small piece of code which you can use in updating your laravel existing migrations.

Run this command to create migration

php artisan make:migration update_users_table
Enter fullscreen mode Exit fullscreen mode
<?php

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

class UpdateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
        $table->string('name', 255)->nullable(true)->change();
        $table->renameColumn('first_name', 'full_name');
        $table->dropColumn('age');
        $table->string('email')->unique()->change();
        $table->integer('gender');
    });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
        $table->string('name', 255)->nullable(false)->change();
        $table->renameColumn('full_name', 'first_name');
        $table->integer('age');
        $table->dropUnique('email');
        $table->dropColumn('gender');
    });
    }
};
Enter fullscreen mode Exit fullscreen mode

Now run this command

run the command php artisan migrate
Enter fullscreen mode Exit fullscreen mode

Please like share and give positive feedback to motivate me to write more.

For more tutorials visit my website.

Thanks:)
Happy Coding:)

Top comments (0)