Skip to content

Commit

Permalink
[5.4] Add Model::refresh() (#19174)
Browse files Browse the repository at this point in the history
*     add model refresh

*    update docblock

*      use find or fail
  • Loading branch information
themsaid authored and taylorotwell committed May 15, 2017
1 parent 26681eb commit ce694c3
Showing 1 changed file with 16 additions and 0 deletions.
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::findOrFail($this->getKey())->attributes);
}

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

2 comments on commit ce694c3

@lucasmichot
Copy link
Contributor

Choose a reason for hiding this comment

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

πŸŽ‰

@ejunker
Copy link
Contributor

Choose a reason for hiding this comment

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

@themsaid Could it return $this so that it works with method chaining? I would like to be able to do this in my tests:

$user = factory(User::class)->create()->refresh();

Please sign in to comment.