Skip to content

[9.x] Accept attribute to touch #36157

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

Merged
merged 2 commits into from
Feb 8, 2021
Merged
Changes from all 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
9 changes: 8 additions & 1 deletion src/Illuminate/Database/Eloquent/Concerns/HasTimestamps.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@ trait HasTimestamps
/**
* Update the model's update timestamp.
*
* @param string|null $attribute
* @return bool
*/
public function touch()
public function touch($attribute = null)
{
if ($attribute) {
Copy link
Contributor

Choose a reason for hiding this comment

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

what would it happen if a given model does not use timestamp? - wouldn't the guard ! $this->usesTimestamps() be necessary for this use case too?

Copy link
Member

@crynobone crynobone Feb 8, 2021

Choose a reason for hiding this comment

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

That should be covered by Line 30. This line covers $model->touch('custom_attribute'); and should affect usesTimestamps().

Copy link
Contributor

Choose a reason for hiding this comment

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

usesTimestamps() only tells you if the Model is using the standard updated_at and created_at, so it should not be checked when $attribute is passed to the touch method.

It is valid to disable the updated/created timestamps (via $timestamps = false) but still use your own custom timestamp column for some other use case, which is when the $attribute argument would be used.

$this->$attribute = $this->freshTimestamp();

return $this->save();
}

if (! $this->usesTimestamps()) {
return false;
}
Expand Down