File tree Expand file tree Collapse file tree 1 file changed +18
-0
lines changed
src/Illuminate/Database/Eloquent Expand file tree Collapse file tree 1 file changed +18
-0
lines changed Original file line number Diff line number Diff line change @@ -477,6 +477,24 @@ public function find($id, $columns = ['*'])
477
477
return $ this ->whereKey ($ id )->first ($ columns );
478
478
}
479
479
480
+ /**
481
+ * Retrieve one or more random model instances.
482
+ *
483
+ * @param int $count
484
+ * @param array $columns
485
+ * @return \Illuminate\Database\Eloquent\Model|null|\Illuminate\Database\Eloquent\Collection
486
+ */
487
+ public function findRandom (int $ count = 1 , array $ columns = ['* ' ])
488
+ {
489
+ if ($ count < 1 ) {
490
+ return $ this ->newModelInstance ()->newCollection ();
491
+ }
492
+
493
+ return $ count === 1
494
+ ? $ this ->inRandomOrder ()->first ($ columns )
495
+ : $ this ->inRandomOrder ()->limit ($ count )->get ($ columns );
496
+ }
497
+
480
498
/**
481
499
* Find a sole model by its primary key.
482
500
*
You can’t perform that action at this time.
0 commit comments