Skip to content

Commit b0c1eb8

Browse files
committed
Add findRand() method to Eloquent Builder for random record selection
1 parent 15596af commit b0c1eb8

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/Illuminate/Database/Eloquent/Builder.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,14 +478,18 @@ public function find($id, $columns = ['*'])
478478
}
479479

480480
/**
481-
* Retrieve a random model instance or a collection of instances.
481+
* Retrieve one or more random model instances.
482482
*
483483
* @param int $count
484484
* @param array $columns
485-
* @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|null
485+
* @return \Illuminate\Database\Eloquent\Model|null|\Illuminate\Database\Eloquent\Collection
486486
*/
487-
public function findRand(int $count = 1, array $columns = ['*'])
487+
public function findRandom(int $count = 1, array $columns = ['*'])
488488
{
489+
if ($count < 1) {
490+
return $this->newModelInstance()->newCollection();
491+
}
492+
489493
return $count === 1
490494
? $this->inRandomOrder()->first($columns)
491495
: $this->inRandomOrder()->limit($count)->get($columns);

0 commit comments

Comments
 (0)