protected function getIndexResponseModels(array $index_response) { $model_collection = $this->whereIn('id', $this->getIdsFromIndexResponse($index_response))->get(); $model_collection = $this->sortIndexResponseModels($model_collection, $index_response['results']); $meta_collection = \Collection::make($index_response['meta']); $facets_collection = \Collection::make($index_response['facets']); $response_collection = \Collection::make([]); $response_collection->put('meta', $meta_collection); $response_collection->put('results', $model_collection); $response_collection->put('facets', $facets_collection); return \Collection::make($response_collection); }
public static function hydrate($class, $results, $type = "collection", $exists = false) { if (!class_exists($class)) { throw new \Exception("class {$class} not exists!"); } elseif ($type == "collection") { $models = array(); foreach ($results as $result) { $model = self::pack($class, $result, $exists); $models[] = $model; } return Collection::make($models); } else { $model = self::pack($class, $results, $exists); return $model; } }
/** * Sort the array using the given callback. * * @param array $array * @param callable $callback * @return array */ public static function sort($array, callable $callback) { return Collection::make($array)->sortBy($callback)->all(); }
/** * 查询集合 * * @param array $field * * @return array */ public function get(array $field = []) { if (!empty($field)) { $this->field($field); } if ($results = $this->query($this->build()->select(), $this->build()->getSelectParams())) { if ($model = $this->getModel()) { $Collection = Collection::make([]); foreach ($results as $k => $v) { $instance = clone $model; $Collection[$k] = $instance->data($v); } return $Collection; } else { return $results; } } }
/** * @param $data * * @return mixed */ function collect($data) { return \Collection::make($data); }