From ca0285fe3daac147b09b0648ac7a265685561c99 Mon Sep 17 00:00:00 2001 From: oleksandrb Date: Mon, 10 Jun 2024 15:52:24 +0300 Subject: [PATCH] Refactor hasColumns method and test --- src/Schema/Builder.php | 11 +++++------ tests/SchemaTest.php | 6 ++++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/Schema/Builder.php b/src/Schema/Builder.php index 3311a7ff6..fdee24df4 100644 --- a/src/Schema/Builder.php +++ b/src/Schema/Builder.php @@ -39,12 +39,11 @@ public function hasColumn($table, $column): bool */ public function hasColumns($table, array $columns): bool { - foreach ($columns as $column) { - if (!$this->hasColumn($table, $column)) { - return false; - } - } - return true; + $collection = $this->connection->table($table); + + return $collection->whereAll($columns, 'exists', true) + ->project(['_id' => 1]) + ->exists(); } /** diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index 5f987b4ee..0e3d44484 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -387,8 +387,10 @@ public function testHasColumn(): void public function testHasColumns(): void { - DB::connection()->collection('newcollection')->insert(['column1' => 'value']); - DB::connection()->collection('newcollection')->insert(['column2' => 'value']); + // Insert documents with both column1 and column2 + DB::connection()->collection('newcollection')->insert([ + ['column1' => 'value1', 'column2' => 'value2'] + ]); $this->assertTrue(Schema::hasColumns('newcollection', ['column1', 'column2'])); $this->assertFalse(Schema::hasColumns('newcollection', ['column1', 'column3']));