File tree Expand file tree Collapse file tree 2 files changed +14
-3
lines changed 
src/Illuminate/Collections Expand file tree Collapse file tree 2 files changed +14
-3
lines changed Original file line number Diff line number Diff line change @@ -979,21 +979,22 @@ public function put($key, $value)
979979     * Get one or a specified number of items randomly from the collection. 
980980     * 
981981     * @param  (callable(TValue): int)|int|null  $number 
982+      * @param  bool  $preserveKeys 
982983     * @return static<int, TValue>|TValue 
983984     * 
984985     * @throws \InvalidArgumentException 
985986     */ 
986-     public  function  random ($ numbernull )
987+     public  function  random ($ numbernull ,  $ preserveKeys  =  false )
987988    {
988989        if  (is_null ($ number
989990            return  Arr::random ($ this items );
990991        }
991992
992993        if  (is_callable ($ number
993-             return  new  static (Arr::random ($ this items , $ number$ this 
994+             return  new  static (Arr::random ($ this items , $ number$ this ,  $ preserveKeys 
994995        }
995996
996-         return  new  static (Arr::random ($ this items , $ number
997+         return  new  static (Arr::random ($ this items , $ number,  $ preserveKeys 
997998    }
998999
9991000    /** 
Original file line number Diff line number Diff line change @@ -2286,9 +2286,19 @@ public function testRandom($collection)
22862286        $ this assertInstanceOf ($ collection$ random
22872287        $ this assertCount (2 , $ random
22882288
2289+         $ random$ datarandom (2 , true );
2290+         $ this assertInstanceOf ($ collection$ random
2291+         $ this assertCount (2 , $ random
2292+         $ this assertCount (2 , array_intersect_assoc ($ randomall (), $ dataall ()));
2293+ 
22892294        $ random$ datarandom (fn  ($ itemsmin (10 , count ($ items
22902295        $ this assertInstanceOf ($ collection$ random
22912296        $ this assertCount (6 , $ random
2297+ 
2298+         $ random$ datarandom (fn  ($ itemsmin (10 , count ($ items1 ), true );
2299+         $ this assertInstanceOf ($ collection$ random
2300+         $ this assertCount (5 , $ random
2301+         $ this assertCount (5 , array_intersect_assoc ($ randomall (), $ dataall ()));
22922302    }
22932303
22942304    /** 
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments