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

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

Merged
merged 5 commits into from
Jan 25, 2023

Conversation

orange-alien
Copy link
Contributor

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

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