ItSolutionStuff.com

How to Drop Primary Key in Laravel Migration?

By Hardik Savani β€’ November 5, 2023
Laravel

Hey Artisan,

If you need to see an example of laravel migration drop primary key. Here you will learn laravel migration drop primary key. We will look at an example of how to remove primary key in laravel migration. let’s discuss about remove primary key in laravel migration.

In this example, i will show you how to drop primary key using laravel migration. we will use drop primary key using dropPrimary() method of laravel migration class.

You will see the following examples:

1. Laravel Migration Add Primary Key using id()

2. Laravel Migration Drop Primary Key

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

Example 1: Laravel Migration Add Primary Key using id()

Here, we will create products table with add Primary Key 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 Drop Primary Key

Here, we will create products table with drop Primary Key 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 β†’
β˜…

How to Add Boolean Column in Laravel Migration?

Read Now β†’
β˜…

How to Change Column Position in Laravel Migration?

Read Now β†’
β˜…

How to Migrate SQL File in Laravel Migration?

Read Now β†’
β˜…

Laravel Migration Execute SQL Query Example

Read Now β†’
β˜…

Laravel Migration Change Datatype Date to Datetime Example

Read Now β†’
β˜…

Laravel Migration Change Default Value Example

Read Now β†’
β˜…

Laravel Migration Change Datatype Text to Longtext Example

Read Now β†’
β˜…

How to Add Index in Laravel Migration?

Read Now β†’
β˜…

How to Change Table Name using Laravel Migration?

Read Now β†’
β˜…

How to Remove Column from Table in Laravel Migration?

Read Now β†’