laravel migration unique column code example

Example 1: laravel migration add unique column

Schema::table('tableName', function($table)
{
    $table->string('column-name')->unique(); //notice the parenthesis I added
});

Example 2: composite unique between two columns laravel migration

public function up()
    {
        Schema::table('user_projects', function (Blueprint $table) {
            $table->unique(["user_id", "project_id"], 'user_project_unique');
        });
    }

    public function down()
    {
        Schema::table('user_projects', function (Blueprint $table) {
          $table->dropUnique('user_project_unique');
        });
    }

Example 3: laravel set field unique

Schema::table('manufacturers', function($table)
{
    $table->string('name')->unique(); 
});

Example 4: laravel create migration

php artisan make:migration add_votes_to_users_table --table=users

php artisan make:migration create_users_table --create=users

Tags:

Php Example