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

[8.x] Add timestamps to model save options #37104

Closed
wants to merge 1 commit into from
Closed

[8.x] Add timestamps to model save options #37104

wants to merge 1 commit into from

Conversation

amir9480
Copy link
Contributor

@amir9480 amir9480 commented Apr 23, 2021

This PR adds disable timestamps option to model save function.

A sample code before this change:

// updating model without touching updated_at
$user->name = 'new name';
$user->timestamps = false;
$user->save();
$user->timestamps = true;

Same code after this change:

$user->name = 'new name';
$user->save(['timestamps' => false]);

Or using update:

$user->update(['name ' => 'new name'], ['timestamps' => false]);

@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If possible, please consider releasing your code as a package so that the community can still take advantage of your contributions!

If you feel absolutely certain that this code corrects a bug in the framework, please "@" mention me in a follow-up comment with further explanation so that GitHub will send me a notification of your response.

@derekmd
Copy link
Contributor

derekmd commented Apr 23, 2021

FYI this was once attempted in Laravel years ago but it ended up being removed because it didn't cover every edge case: #12617 (#12403 (comment))

If you don't wish to update timestamps on model relationships, the (currently undocumented) Model::withoutTouching() method may be what you want: #23469

From your code sample above updating a single model, userland can accomplish this by adding a method to the Model class.

namespace App\Models;

use Illuminate\Database\Eloquent\Model as BaseModel;

class Model extends BaseModel
{
    public function keepingTimestamps(callable $callback)
    {
        try {
            $timestamps = $this->timestamps;
            $this->timestamps = false;

            return tap($this, $callback);
        } finally {
            $this->timestamps = $timestamps;
        }
    }
}

App model classes will extend App\Models\Model instead of Illuminate\Database\Eloquent\Model. Or if only one model needs this behavior, define this method in that class.

optional($request->user()->latestMessage)->keepingTimestamps(
    fn ($message) => $message->update(['read_at' => now()])
);

@patrickomeara
Copy link
Contributor

See #43441

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants