Illuminate\Support\Collection::shuffle PHP Method

shuffle() public method

Shuffle the items in the collection.
public shuffle ( integer $seed = null ) : static
$seed integer
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);
    }

Usage Example

 public function getAmaotoFlow()
 {
     $flow = new Collection();
     Music::orderByRaw('RAND()')->take(10)->get()->each(function ($item) use(&$flow) {
         /** @var Music $item */
         $flow->push(['type' => 'music', 'data' => $item->toArray()]);
     });
     Album::orderByRaw('RAND()')->take(10)->get()->each(function ($item) use(&$flow) {
         /** @var Album $item */
         $flow->push(['type' => 'album', 'data' => $item->toArray()]);
     });
     $result = $flow->shuffle();
     return $this->buildResponse(trans('api.system.get-amaoto-flow.success'), Tools::toArray($result));
 }