Skip to content

Instantly share code, notes, and snippets.

@marlocorridor
Last active December 15, 2016 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marlocorridor/3731d48af3353330d3b086276131c3fe to your computer and use it in GitHub Desktop.
Save marlocorridor/3731d48af3353330d3b086276131c3fe to your computer and use it in GitHub Desktop.
Check if index exist

Laravel Migration Check Foreign Key if exist tested on laravel 5.0

	 /**
	 * Reverse the migrations.
	 *
	 * @return void
	 */
	public function down()
	{
		Schema::table('admin', function(Blueprint $table)
		{
			$schema_builder = Schema::getConnection()
				->getDoctrineSchemaManager()
				->listTableDetails( $table->getTable() );

			if( $schema_builder->hasIndex('admin_ibfk_1') )
				$table->dropForeign('admin_ibfk_1');

			if( $schema_builder->hasIndex('admin_ibfk_2') )
				$table->dropForeign('admin_ibfk_2');
		});
	}

sample usage base on @guiwoda

laravel/framework#3253

$conn = Schema::getConnection();
$dbSchemaManager = $conn->getDoctrineSchemaManager();
$doctrineTable = $dbSchemaManager->listTableDetails('users');

// alter table "users" add constraint users_email_unique unique ("email")
if (! $doctrineTable->hasIndex('users_email_unique'))
{
    $table->unique('email');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment