Example #1
0
 /**
  * @inheritdoc
  */
 public function __construct($items = null, $class_slug = null, $key_by = null)
 {
     $items = is_null($items) ? [] : $items;
     if ($items instanceof \Illuminate\Support\Collection) {
         return parent::__construct($items);
     }
     if ($class_slug) {
         $items = array_map(function ($item) use($class_slug) {
             if (is_null($item)) {
                 return $item;
             }
             if (!is_array($item)) {
                 throw new \Exception("Failed to cast a model");
             }
             $class = Model::getModelClass($class_slug);
             return new $class($item);
         }, $items);
     }
     if ($key_by) {
         $items = array_build($items, function ($key, $value) use($key_by) {
             /** @var Model $value */
             $key = $value instanceof Model ? $value->get($key_by) : data_get($value, $key_by);
             return [$key, $value];
         });
     }
     return parent::__construct($items);
 }