diff --git a/src/Query.php b/src/Query.php index 9c0d90a2..28a36a7e 100644 --- a/src/Query.php +++ b/src/Query.php @@ -31,11 +31,19 @@ public function __construct( */ public function find(?string $id): ?Model { - if (! is_null($id)) { + if (is_null($id)) { + return null; + } + + if (! empty($this->model->getSearchable())) { return $this->whereKey($id)->first(); } - return null; + if (! $this->cache->exists($hash = $this->model->getBaseHashWithKey($id))) { + return null; + } + + return $this->newModelFromHash($hash); } /** @@ -218,10 +226,7 @@ public function chunk(int $count, Closure $callback): void $models = $this->model->newCollection(); foreach ($chunk as $hash) { - $models->add($this->model->newFromBuilder([ - ...$this->cache->getAttributes($hash), - $this->model->getKeyName() => $this->getKeyValue($hash), - ])); + $models->add($this->newModelFromHash($hash)); } if ($callback($models) === false) { @@ -230,6 +235,17 @@ public function chunk(int $count, Closure $callback): void } } + /** + * Create a new model instance from the given hash. + */ + protected function newModelFromHash(string $hash): Model + { + return $this->model->newFromBuilder([ + ...$this->cache->getAttributes($hash), + $this->model->getKeyName() => $this->getKeyValue($hash), + ]); + } + /** * Get the model key's value from the given hash. */