How to Drop Unique Constraint in Laravel Migration?
Hi Folks,
In this article, we will cover how to drop unique constraint in laravel migration. I explained simply step by step laravel migration drop unique constraint. you can understand a concept of laravel migration drop unique. This example will help you laravel migration remove unique. you will do the following things for laravel migration remove unique constraint.
The Laravel migration provides the unique() method to add a SQL unique constraint. Once you have added the unique constraint, you can also drop it. Here is a simple example of how to drop a unique constraint in Laravel migration using the dropUnique() method.
You will see the following examples:
1. Laravel Migration Add Unique Constraint
2. Laravel Migration Drop Unique Constraint
let's see the one by one.
Example 1: Laravel Migration Add Unique Constraint
Here, we will create products table with add Unique Constraint to code 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->string('code')->unique();
$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 Unique Constraint
Here, we will create products table with drop Unique Constraint to code column.
Create new migration using following command:
php artisan make:migration change_datatype_column
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->dropUnique('products_code_unique');
});
}
/**
* 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
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Add Unique Constraint in Laravel Migration?
- How to Add Boolean Column in Laravel Migration?
- How to Change Column Position in Laravel Migration?
- How to Migrate SQL File in Laravel Migration?
- Laravel Migration Execute SQL Query Example
- Laravel Migration Remove Default Value Example
- Laravel Migration Change Datatype Integer to Decimal Example
- How to Create Migration in Laravel 10?
- Laravel Migration Add Column After Column Example
- Laravel Migration Default Value Current Timestamp Example
- Laravel Migration Add Enum Column Example
- How to add Default Value of Column in Laravel Migration?
- Laravel Migration Custom Index Name Example