-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[5.4] Add Model::refresh() #19174
Conversation
Woo! |
Wondering if there is a way to not have to call |
refresh() will reload all relationships currently loaded automatically. |
got it, cool! |
@themsaid what is the different between fresh and refresh here? couldn't refresh just call fresh? |
@morloderex fresh loads relations based on $with parameter, refresh reloads all relations based on current model loaded relations. |
@@ -944,6 +944,23 @@ public function fresh($with = []) | |||
} | |||
|
|||
/** | |||
* Reload the current model instance with fresh attributes from the database. | |||
* | |||
* @param array|string $with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No param
|
||
$this->load(array_keys($this->relations)); | ||
|
||
$this->setRawAttributes(static::find($this->getKey())->attributes); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Where are unit tests? |
I meant if it was deleted somewhere else, not in this request.
|
@JosephSilber good point, switched to using |
@themsaid Why return view('admin.selections.list-item')->with('selection', $selection->refresh()); |
@rorc you can use |
@themsaid But, if i want reload only current relations? |
@rorc just call |
@themsaid Sigh, except 1 line i have to use 2 lines? Are you kidding? 😒 $selection->refresh();
return view('admin.selections.list-item')->with('selection', $selection); |
@rorc haha .. well I think I have an idea |
@themsaid Why not just return the refreshed model? 😉 |
plus 1 vote for Very useful for chaining in tests. $comment->refresh(); // redundant line here!
$this->assertCount(1, $comment->replies); |
This method eliminates the need for:
You can do this instead: