ItSolutionStuff.com

Laravel Migration Remove Auto Increment Example

By Hardik Savani • November 5, 2023
Laravel

Hello everyone,

Welcome to this tutorial, where our primary focus will be on removing auto-increment functionality in Laravel migrations. Throughout this guide, I will provide you with a straightforward example demonstrating how to achieve this task within your Laravel migration process. I'll break down the steps for removing auto-increment in Laravel migrations in a clear and concise manner. Our goal is to understand the process of migrating and modifying the auto-increment feature in Laravel effectively. Let's jump right in and explore the intricacies of Laravel's auto-increment removal in migrations.

Laravel migration provides method to remove auto increment from id column. we will use unsigned() and dropPrimary() method to remove auto increment column.

You will see the following examples:

1. Laravel Migration Add Auto Increment

2. Laravel Migration Remove Auto Increment

So, let's see the following one by one example:

Example 1: Laravel Migration Add Auto Increment

Here, we will create products table with add Auto Increment to id column.

Create new migration using following command:

php artisan make:migration create_products_table

Now, You can update it as like the bellow:

database/migrations/migration_name.php

<?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('products', function (Blueprint $table) {

$table->id();

$table->string('name');

$table->text('description');

$table->timestamps();

});

}

/**

* Reverse the migrations.

*/

public function down(): void

{

Schema::dropIfExists('products');

}

};

Now, you are ready to run migration command:

php artisan migrate

You will see the layout as like the below:

Example 2: Laravel Migration Remove Auto Increment

Here, we will create products table with drop Auto Increment to id column.

First of all we need to install "doctrine/dbal" composer package. This package allow to use change() method to update datatype using laravel migration.

composer require doctrine/dbal

Create new migration using following command:

php artisan make:migration drop_primary_key

Now, You can update it as like the bellow:

database/migrations/migration_name.php

<?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('products', function (Blueprint $table) {

$table->integer('id')->unsigned()->change();

$table->dropPrimary('id');

});

}

/**

* Reverse the migrations.

*/

public function down(): void

{

Schema::dropIfExists('products');

}

};

Now, you are ready to run migration command:

php artisan migrate

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Drop Unique Constraint in Laravel Migration?

Read Now →

Laravel Migration Unique Multiple Columns Example

Read Now →

How to Add Unique Constraint in Laravel Migration?

Read Now →

Laravel Migration Change Datatype String to Integer Example

Read Now →

Laravel Migration Add Column After Column Example

Read Now →

Laravel Migration Default Value Current Timestamp Example

Read Now →

How to Create Migration in Laravel 9?

Read Now →

How to Update Enum Value in Laravel Migration?

Read Now →

Laravel Migration Enum Default Value Example

Read Now →

Laravel Migration Add Enum Column Example

Read Now →

Laravel Migration Add Comment to Column Example

Read Now →

How to Remove Column from Table in Laravel Migration?

Read Now →

How to Create Table using Migration in Laravel?

Read Now →

How to Drop Foreign Key Constraint in Laravel Migration?

Read Now →