File tree Expand file tree Collapse file tree 2 files changed +5
-10
lines changed Expand file tree Collapse file tree 2 files changed +5
-10
lines changed Original file line number Diff line number Diff line change 8
8
use MongoDB \Model \CollectionInfo ;
9
9
use MongoDB \Model \IndexInfo ;
10
10
11
+ use function array_fill_keys ;
11
12
use function array_keys ;
12
13
use function assert ;
13
14
use function count ;
@@ -25,31 +26,24 @@ class Builder extends \Illuminate\Database\Schema\Builder
25
26
*
26
27
* @param string $table
27
28
* @param string $column
28
- *
29
- * @return bool
30
29
*/
31
30
public function hasColumn ($ table , $ column ): bool
32
31
{
33
- $ collection = $ this ->connection ->table ($ table );
34
-
35
- return $ collection ->where ($ column , 'exists ' , true )
36
- ->project (['_id ' => 1 ])
37
- ->exists ();
32
+ return $ this ->hasColumns ($ table , [$ column ]);
38
33
}
39
34
40
35
/**
41
36
* Check if columns exists in the collection schema.
42
37
*
43
38
* @param string $table
44
39
* @param string[] $columns
45
- *
46
- * @return bool
47
40
*/
48
41
public function hasColumns ($ table , array $ columns ): bool
49
42
{
50
43
$ collection = $ this ->connection ->table ($ table );
51
44
52
- return $ collection ->whereAll ($ columns , 'exists ' , true )
45
+ return $ collection
46
+ ->where (array_fill_keys ($ columns , ['$exists ' => true ]))
53
47
->project (['_id ' => 1 ])
54
48
->exists ();
55
49
}
Original file line number Diff line number Diff line change @@ -395,6 +395,7 @@ public function testHasColumns(): void
395
395
// Insert documents with both column1 and column2
396
396
DB ::connection ()->collection ('newcollection ' )->insert ([
397
397
['column1 ' => 'value1 ' , 'column2 ' => 'value2 ' ],
398
+ ['column1 ' => 'value3 ' ],
398
399
]);
399
400
400
401
$ this ->assertTrue (Schema::hasColumns ('newcollection ' , ['column1 ' , 'column2 ' ]));
You can’t perform that action at this time.
0 commit comments