File tree Expand file tree Collapse file tree 3 files changed +44
-7
lines changed Expand file tree Collapse file tree 3 files changed +44
-7
lines changed Original file line number Diff line number Diff line change 11# Changelog  
22All notable changes to this project will be documented in this file.
33
4- ## [ 4.9 .0]  - coming soon 
4+ ## [ 4.7 .0]  - coming soon 
55
66*  Add ` Connection::getServerVersion() `  by @GromNaN   in [ #3043  ] ( https://github.com/mongodb/laravel-mongodb/pull/3043 ) 
77*  Add ` Schema\Builder::getTables() `  and ` getTableListing() `  by @GromNaN   in [ #3044  ] ( https://github.com/mongodb/laravel-mongodb/pull/3044 ) 
88*  Add ` Schema\Builder::getColumns() `  and ` getIndexes() `  by @GromNaN   in [ #3045  ] ( https://github.com/mongodb/laravel-mongodb/pull/3045 ) 
9+ *  Add ` Schema\Builder::hasColumn `  and ` hasColumns `  method by @Alex-Belyi   in [ #3002  ] ( https://github.com/mongodb/laravel-mongodb/pull/3001 ) 
910
1011## [ 4.6.0]  - 2024-07-09 
1112
Original file line number Diff line number Diff line change 88use  MongoDB \Model \CollectionInfo ;
99use  MongoDB \Model \IndexInfo ;
1010
11+ use  function  array_fill_keys ;
1112use  function  array_keys ;
1213use  function  assert ;
1314use  function  count ;
2021
2122class  Builder extends  \Illuminate \Database \Schema \Builder
2223{
23-     /** @inheritdoc */ 
24-     public  function  hasColumn ($ table$ column
24+     /** 
25+      * Check if column exists in the collection schema. 
26+      * 
27+      * @param string $table 
28+      * @param string $column 
29+      */ 
30+     public  function  hasColumn ($ table$ columnbool 
2531    {
26-         return  true ;
32+         return  $ this -> hasColumns ( $ table , [ $ column ]) ;
2733    }
2834
29-     /** @inheritdoc */ 
30-     public  function  hasColumns ($ tablearray  $ columns
35+     /** 
36+      * Check if columns exists in the collection schema. 
37+      * 
38+      * @param string   $table 
39+      * @param string[] $columns 
40+      */ 
41+     public  function  hasColumns ($ tablearray  $ columnsbool 
3142    {
32-         return  true ;
43+         $ collection$ this connection ->table ($ table
44+ 
45+         return  $ collection
46+             ->where (array_fill_keys ($ columns'$exists '  => true ]))
47+             ->project (['_id '  => 1 ])
48+             ->exists ();
3349    }
3450
3551    /** 
Original file line number Diff line number Diff line change @@ -382,6 +382,26 @@ public function testRenameColumn(): void
382382        $ this assertSame ($ check2 ]['column ' ], $ check22 ]['column ' ]);
383383    }
384384
385+     public  function  testHasColumn (): void 
386+     {
387+         DB ::connection ()->collection ('newcollection ' )->insert (['column1 '  => 'value ' ]);
388+ 
389+         $ this assertTrue (Schema::hasColumn ('newcollection ' , 'column1 ' ));
390+         $ this assertFalse (Schema::hasColumn ('newcollection ' , 'column2 ' ));
391+     }
392+ 
393+     public  function  testHasColumns (): void 
394+     {
395+         // Insert documents with both column1 and column2 
396+         DB ::connection ()->collection ('newcollection ' )->insert ([
397+             ['column1 '  => 'value1 ' , 'column2 '  => 'value2 ' ],
398+             ['column1 '  => 'value3 ' ],
399+         ]);
400+ 
401+         $ this assertTrue (Schema::hasColumns ('newcollection ' , ['column1 ' , 'column2 ' ]));
402+         $ this assertFalse (Schema::hasColumns ('newcollection ' , ['column1 ' , 'column3 ' ]));
403+     }
404+ 
385405    public  function  testGetTables ()
386406    {
387407        DB ::connection ('mongodb ' )->collection ('newcollection ' )->insert (['test '  => 'value ' ]);
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments