diff --git a/src/Illuminate/Database/Schema/Builder.php b/src/Illuminate/Database/Schema/Builder.php index 9cb687f97e8a..51b97f690618 100755 --- a/src/Illuminate/Database/Schema/Builder.php +++ b/src/Illuminate/Database/Schema/Builder.php @@ -403,27 +403,15 @@ public function hasIndex($table, $index, $type = null) { $type = is_null($type) ? $type : strtolower($type); - if (is_array($index)) { - sort($index); - } - foreach ($this->getIndexes($table) as $value) { $typeMatches = is_null($type) || ($type === 'primary' && $value['primary']) || ($type === 'unique' && $value['unique']) || $type === $value['type']; - if ($value['name'] === $index && $typeMatches) { + if (($value['name'] === $index || $value['columns'] === $index) && $typeMatches) { return true; } - - if (is_array($index)) { - sort($value['columns']); - - if ($value['columns'] === $index && $typeMatches) { - return true; - } - } } return false; diff --git a/tests/Integration/Database/SchemaBuilderTest.php b/tests/Integration/Database/SchemaBuilderTest.php index 7a75d62b5fe0..6fd62e399cfc 100644 --- a/tests/Integration/Database/SchemaBuilderTest.php +++ b/tests/Integration/Database/SchemaBuilderTest.php @@ -288,9 +288,9 @@ public function testGetUniqueIndexes() )); $this->assertTrue(Schema::hasIndex('foo', 'foo_baz_bar_unique')); $this->assertTrue(Schema::hasIndex('foo', 'foo_baz_bar_unique', 'unique')); - $this->assertTrue(Schema::hasIndex('foo', ['bar', 'baz'])); - $this->assertTrue(Schema::hasIndex('foo', ['bar', 'baz'], 'unique')); - $this->assertFalse(Schema::hasIndex('foo', ['bar', 'baz'], 'primary')); + $this->assertTrue(Schema::hasIndex('foo', ['baz', 'bar'])); + $this->assertTrue(Schema::hasIndex('foo', ['baz', 'bar'], 'unique')); + $this->assertFalse(Schema::hasIndex('foo', ['baz', 'bar'], 'primary')); } public function testGetIndexesWithCompositeKeys() @@ -335,6 +335,26 @@ public function testGetFullTextIndexes() $this->assertTrue(collect($indexes)->contains('name', 'articles_body_title_fulltext')); } + public function testHasIndexOrder() + { + Schema::create('foo', function (Blueprint $table) { + $table->integer('bar'); + $table->integer('baz'); + $table->integer('qux'); + + $table->unique(['bar', 'baz']); + $table->index(['baz', 'bar']); + $table->index(['baz', 'qux']); + }); + + $this->assertTrue(Schema::hasIndex('foo', ['bar', 'baz'])); + $this->assertTrue(Schema::hasIndex('foo', ['bar', 'baz'], 'unique')); + $this->assertTrue(Schema::hasIndex('foo', ['baz', 'bar'])); + $this->assertFalse(Schema::hasIndex('foo', ['baz', 'bar'], 'unique')); + $this->assertTrue(Schema::hasIndex('foo', ['baz', 'qux'])); + $this->assertFalse(Schema::hasIndex('foo', ['qux', 'baz'])); + } + public function testGetForeignKeys() { Schema::create('users', function (Blueprint $table) {