라라벨 - Collection, Query 정보를 Random 반환

Collection

shuffle()

/**
 * Shuffle the items in the collection.
 *
 * @param  int  $seed
 * @return static
 */
public function shuffle($seed = null)
{
    $items = $this->items;

    if (is_null($seed)) {
        shuffle($items);
    } else {
        srand($seed);

        usort($items, function () {
            return rand(-1, 1);
        });
    }

    return new static($items);
}

Query

inRandomOrder

/**
 * Put the query's results in random order.
 *
 * @param string $seed
 * @return $this
 */
public function inRandomOrder($seed = '')
{
	return $this->orderByRaw($this->grammar->compileRandom($seed));
}
/**
 * Compile the random statement into SQL.
 *
 * @paramstring $seed
 * @return string
 */
public function compileRandom($seed)
{
	return 'RANDOM()';
}
  • share