Skip to content

[9.x] Add "addRestoreOrCreate" extension to SoftDeletingScope - #45754

Merged
taylorotwell merged 5 commits into
laravel:9.xfrom
orange-alien:add_restore_or_create
Jan 25, 2023
Merged

[9.x] Add "addRestoreOrCreate" extension to SoftDeletingScope#45754
taylorotwell merged 5 commits into
laravel:9.xfrom
orange-alien:add_restore_or_create

Conversation

@orange-alien

@orange-alien orange-alien commented Jan 21, 2023

Copy link
Copy Markdown
Contributor

I would like to suggest adding addRestoreOrCreate extenstion to the IlluminateDatabaseEloquentSoftDeletingScope.

Following exampels are assumed that User model use SofteDelete trait.

before this PR

  • We need to check if user exists or not and do restore() or create().

$user = User::withTrashed()
    ->where(['email' => 'user@example.com'])
    ->first();

if($user) {
    $user->restore();
} else {
    $user = User::create([
        'email' => 'user@example.com',
        'name' => 'user name',
    ]);
}

after this PR

  • We don't have to worry about if user exist or not.

$user = User::restoreOrCretae(
    ['email' => 'user@example.com'],
    ['name' => 'user name'],
);

@orange-alien orange-alien changed the title Add "addRestoreOrCreate" extension to SoftDeletingScope [9.x]Add "addRestoreOrCreate" extension to SoftDeletingScope Jan 21, 2023
@orange-alien orange-alien changed the title [9.x]Add "addRestoreOrCreate" extension to SoftDeletingScope [9.x] Add "addRestoreOrCreate" extension to SoftDeletingScope Jan 21, 2023
@taylorotwell
taylorotwell merged commit bbed00b into laravel:9.x Jan 25, 2023
@orange-alien
orange-alien deleted the add_restore_or_create branch January 25, 2023 22:14
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.

2 participants