Skip to content

Commit

Permalink
add setKeysForSelectQuery method and use it when refreshing model data
Browse files Browse the repository at this point in the history
  • Loading branch information
adamthehutt committed Oct 26, 2020
1 parent 6defe37 commit 15ee5e6
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/Illuminate/Database/Eloquent/Model.php
Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand Down Expand Up @@ -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();
}
Expand All @@ -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) {
Expand Down

0 comments on commit 15ee5e6

Please sign in to comment.