ItSolutionStuff.com

Laravel Migration Unique Multiple Columns Example

By Hardik Savani • November 5, 2023
Laravel

Hey Folks,

In this short tutorial, we will share the quick and straightforward way to laravel unique constraint multiple columns. This post will give you a simple example of laravel migration unique multiple columns. This example will help you make multiple column unique laravel migration. we will help you to give an example of how to add multiple unique constraint in laravel migration.

Laravel migration offers the unique() method as a means to include a sql unique constraint for multiple columns. I will now provide a straightforward example of how to incorporate a multiple unique constraint through Laravel migration. Additionally, you can also add multiple unique constraints and remove existing ones. Let us now delve into a simple illustration of this process.

You will see the following examples:

1. Laravel Migration Add Unique Constraint

2. Laravel Migration Add Multiple Unique Constraint

3. Laravel Migration Add Multiple Unique Constraint 2

4. 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 Add Multiple Unique Constraint

Here, we will create products table with add Unique Constraint to code and contact_email column.

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');

$table->string('contact_email');

$table->text('description');

$table->timestamps();

$table->unique('code');

$table->unique('contact_email');

});

}

/**

* 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 3: Laravel Migration Add Multiple Unique Constraint 2

Here, we will create products table with add Unique Constraint to code and contact_email 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');

$table->string('contact_email');

$table->text('description');

$table->timestamps();

$table->unique(['code', 'contact_email']);

});

}

/**

* 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 4: 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

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 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 Remove Default Value Example

Read Now →

Laravel Migration Change Datatype Timestamp to Datetime 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 String to Integer Example

Read Now →

How to Drop Soft Delete from Table using Laravel Migration?

Read Now →

Laravel Migration Add Column After Column Example

Read Now →

Laravel Migration Default Value Current Timestamp Example

Read Now →

How to Update Enum Value in Laravel Migration?

Read Now →

Laravel Migration Custom Foreign Key Name Example

Read Now →

How to Remove Column from Table in Laravel Migration?

Read Now →