DEV Community

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

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

11 1

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:)

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay