Skip to content

Commit

Permalink
added collection without DB
Browse files Browse the repository at this point in the history
  • Loading branch information
LonnyX committed Mar 31, 2019
1 parent 41042a2 commit 46ec373
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/ElasticEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@ public function map(Builder $builder, $results, $model)
if ($this->getTotalCount($results) == 0) {
return Collection::make();
}
if($model->collectWithoutDB) {
return $this->collectWithoutDB($results, $model);
}

$primaryKey = $model->getKeyName();

Expand Down Expand Up @@ -323,6 +326,21 @@ public function map(Builder $builder, $results, $model)
->values();
}

public function collectWithoutDB($results, $model)
{
return Collection::make($results['hits']['hits'])
->map(function ($hit) use ($model) {
$model = (new $model());
$model->id = $hit['_id'];
foreach($hit[ '_source'] as $key => $value) {
$model->{$key} = is_array($value) ? (object)$value : $value;
}
return $model;
})
->filter()
->values();
}

/**
* @inheritdoc
*/
Expand Down

0 comments on commit 46ec373

Please sign in to comment.