Laravel Migration Make Column Auto Increment

Migration make column auto increment; In this tutorial, we are going to show you how to make column auto increment using migration in laravel 10, 9, 8, 7 apps.

Laravel provides an easy way to define database tables, columns, and other schema elements using migration files. Migration files allow developers to version control the changes in the database schema, and Laravel’s migration system makes it easy to migrate and rollback changes. In this article, we will discuss how to make an auto-incrementing column in a Laravel migration.

Laravel Migration Make Column Auto Increment

Use the below given steps for how to make a column auto-increment in Laravel migration:

  • Step 1: Create a Migration File
  • Step 2: Define Table Schema
  • Step 3: Make Column Auto-Increment

Step 1: Create a Migration File

The first step is to create a migration file using the make:migration Artisan command. We can use the following command to create a migration file:

php artisan make:migration create_table_name

In the above command, create_table_name is the name of the migration file. Once we run the command, Laravel creates a migration file under the database/migrations directory.

Step 2: Define Table Schema

In the migration file, we define the table schema using the Schema facade. We can use the create method to create a new table and the table method to modify an existing table. Here’s an example of creating a new table:

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

class CreateTable extends Migration
{
    public function up()
    {
        Schema::create('table_name', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('table_name');
    }
}

In the above migration file, we have defined a new table table_name with two columns id and name. The increments method creates an auto-incrementing integer column named id.

Step 3: Make Column Auto-Increment

To make a column auto-increment, we need to use the unsignedBigInteger method instead of the increments method. Here’s an example of making the id column auto-increment:

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

class CreateTable extends Migration
{
    public function up()
    {
        Schema::create('table_name', function (Blueprint $table) {
            $table->unsignedBigInteger('id')->autoIncrement();
            $table->string('name');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('table_name');
    }
}

In the above migration file, we have used the unsignedBigInteger method instead of the increments method to create the id column. The autoIncrement method makes the id column auto-incrementing.

Conclusion

Making a column auto-increment is a common task when creating a database table. In Laravel migration system, we can use the unsignedBigInteger method with the autoIncrement method to create an auto-incrementing column. In this article, we have discussed how to make a column auto-increment in Laravel migration. The Laravel migration system is a powerful tool that enables developers to manage database schema changes efficiently.

More Tutorials

Leave a Comment