How to Add Primary Key in Laravel 10 Migration

Hey there, Laravel enthusiasts! If you're diving into the world of Laravel 10 migrations and find yourself wondering how to wield the power of primary keys, you're in the right place. Primary keys play a crucial role in database design, and Laravel makes it a breeze to manage them through migrations.

In this article, we'' see how to add a primary key in laravel 10 migration, laravel 10 migration add primary key, how to specify a primary key in laravel 8/9/10, laravel migration adds a primary key.

In this guide, I'll walk you through three different methods to handle primary keys in your migrations. Whether you prefer the default simplicity of id(), want to customize your primary key using primary(), or need to drop a primary key altogether, we've got you covered 🚀

Below is a step-by-step guide on how to add a primary key in Laravel 10 migration using three different methods: id(), primary(), and dropping a primary key.

1. Laravel Migration Add Primary Key using id()

The id() method is a convenient way to add an auto-incrementing primary key to a table.

<?php

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

class CreateExampleTable extends Migration
{
    public function up()
    {
        Schema::create('examples', function (Blueprint $table) {
            $table->id(); // This adds an auto-incrementing primary key
            $table->string('name');
            $table->timestamps();
        });
    }

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

 

2. Laravel Migration Add Primary Key using primary()

The primary() method allows you to specify a custom column as the primary key.

<?php

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

class CreateExampleTable extends Migration
{
    public function up()
    {
        Schema::create('examples', function (Blueprint $table) {
            $table->bigIncrements('custom_id'); // Custom primary key
            $table->primary('custom_id'); // Specify the column as primary key
            $table->string('name');
            $table->timestamps();
        });
    }

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

 

3. Laravel Migration Drop Primary Key

If you need to drop a primary key from a table, you can use the dropPrimary method.

Install Doctrine/DBAL:

If you haven't installed Doctrine/DBAL yet, you can do so using Composer.

composer require doctrine/dbal

Create the Migration:

Now, create a migration using Laravel's artisan command:

php artisan make:migration drop_primary_key_from_example_table

Modify the Migration File:

Open the generated migration file and use Doctrine/DBAL to drop the primary key:

<?php

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

class DropPrimaryKeyFromExampleTable extends Migration
{
    public function up(): void
    {
        Schema::table('examples', function (Blueprint $table) {
            $table->integer('custom_id')->unsigned()->change();
            $table->dropPrimary('custom_id');
        });
    }

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

Run the Migration:

Finally, run the migration to apply the changes to the database:

php artisan migrate

 


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS