From 15ee5e62861dc20275cf9f75b12838948bc35808 Mon Sep 17 00:00:00 2001 From: Adam Huttler Date: Mon, 26 Oct 2020 10:38:45 -1000 Subject: [PATCH] add setKeysForSelectQuery method and use it when refreshing model data --- src/Illuminate/Database/Eloquent/Model.php | 27 ++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Model.php b/src/Illuminate/Database/Eloquent/Model.php index a23c46350b8b..c47d801bfbc3 100644 --- a/src/Illuminate/Database/Eloquent/Model.php +++ b/src/Illuminate/Database/Eloquent/Model.php @@ -826,6 +826,19 @@ protected function setKeysForSaveQuery($query) return $query; } + /** + * Set the keys for a select query. + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @return \Illuminate\Database\Eloquent\Builder + */ + protected function setKeysForSelectQuery($query) + { + $query->where($this->getKeyName(), '=', $this->getKeyForSelectQuery()); + + return $query; + } + /** * Get the primary key value for a save query. * @@ -836,6 +849,16 @@ protected function getKeyForSaveQuery() return $this->original[$this->getKeyName()] ?? $this->getKey(); } + /** + * Get the primary key value for a select query. + * + * @return mixed + */ + protected function getKeyForSelectQuery() + { + return $this->original[$this->getKeyName()] ?? $this->getKey(); + } + /** * Perform a model insert operation. * @@ -1214,7 +1237,7 @@ public function fresh($with = []) return; } - return $this->setKeysForSaveQuery(static::newQueryWithoutScopes()) + return $this->setKeysForSelectQuery(static::newQueryWithoutScopes()) ->with(is_string($with) ? func_get_args() : $with) ->first(); } @@ -1231,7 +1254,7 @@ public function refresh() } $this->setRawAttributes( - $this->setKeysForSaveQuery(static::newQueryWithoutScopes())->firstOrFail()->attributes + $this->setKeysForSelectQuery(static::newQueryWithoutScopes())->firstOrFail()->attributes ); $this->load(collect($this->relations)->reject(function ($relation) {