Skip to content

[9.x] Add ability to discard Eloquent Model changes#43772

Merged
taylorotwell merged 2 commits into
laravel:9.xfrom
crynobone:eloquent-discard-changes
Sep 8, 2022
Merged

[9.x] Add ability to discard Eloquent Model changes#43772
taylorotwell merged 2 commits into
laravel:9.xfrom
crynobone:eloquent-discard-changes

Conversation

@crynobone

@crynobone crynobone commented Aug 19, 2022

Copy link
Copy Markdown
Member
Project::updating(function (Project $model) {
    /*
     * The project update should be a change request
     */
    $changeRequest = new ProjectChangeRequest([
        'original' => array_intersect_key($model->getRawOriginal(), $model->getDirty()),
        'changes' => $model->getDirty(),
    ]);

    $changeRequest->source_model()->associate($model);
    $changeRequest->save();

    // Now we can simply ignore all attribute changes and only save `status="change_request"`
    $model->discardChanges();

    $model->status = 'change_request';
});

Signed-off-by: Mior Muhammad Zaki crynobone@gmail.com

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
@crynobone crynobone changed the title [9.x] Add ability to discard Eloquent Model changes. [9.x] Add ability to discard Eloquent Model changes Aug 19, 2022
Comment thread src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Comment thread tests/Database/DatabaseEloquentModelTest.php
Comment thread tests/Database/DatabaseEloquentModelTest.php
Comment thread tests/Integration/Database/EloquentModelTest.php
Comment thread tests/Integration/Database/EloquentModelTest.php
@bernardwiesner

Copy link
Copy Markdown
Contributor

Seems almost same as this PR: #37756

@bert-w

bert-w commented Aug 23, 2022

Copy link
Copy Markdown
Contributor

Maybe add an $options parameter to add except/only functionality? Somewhat similar to:

protected function getResourceMethods($defaults, $options)
{
$methods = $defaults;
if (isset($options['only'])) {
$methods = array_intersect($methods, (array) $options['only']);
}
if (isset($options['except'])) {
$methods = array_diff($methods, (array) $options['except']);
}
return $methods;
}

@taylorotwell
taylorotwell merged commit 71034a4 into laravel:9.x Sep 8, 2022
@crynobone
crynobone deleted the eloquent-discard-changes branch September 9, 2022 01:43
@amitmerchant1990

Copy link
Copy Markdown
Contributor

Hi!

Just curious about how is it different from refreshing models.

@rodrigopedra

Copy link
Copy Markdown
Contributor

@amitmerchant1990

refresh() reloads the model data from the database. You do a database call, and can get attributes that might have changed from another requests in the meanwhile.

discardChanges() will, eh, discard any unsaved changes, and return the model to its original state, without performing a database call.

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.

9 participants