Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.4] Add Model::refresh() #19174

Merged
merged 3 commits into from
May 15, 2017
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,22 @@ public function fresh($with = [])
->first();
}

/**
* Reload the current model instance with fresh attributes from the database.
*
* @return void
*/
public function refresh()
{
if (! $this->exists) {
return;
}

$this->load(array_keys($this->relations));

$this->setRawAttributes(static::find($this->getKey())->attributes);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be findOrFail? What should happen if the model has been deleted in the meantime?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the model was deleted exists would return false and the method will just bail.

}

/**
* Clone the model into a new, non-existing instance.
*
Expand Down