Skip to content

[9.x] Add a new function onlyTrashed - #44989

Merged
taylorotwell merged 3 commits into
laravel:9.xfrom
michaelnabil230:DatabaseRule
Nov 17, 2022
Merged

[9.x] Add a new function onlyTrashed#44989
taylorotwell merged 3 commits into
laravel:9.xfrom
michaelnabil230:DatabaseRule

Conversation

@michaelnabil230

@michaelnabil230 michaelnabil230 commented Nov 17, 2022

Copy link
Copy Markdown
Contributor

This PR introduces an "onlyTrashed" validation rule in Exists and Unique.

For example, we want a user to be able to create a new row in the database user, but we want to search in the database where the deleted_at is not null.
And the same since the same function is in the same trait SoftDeletes

Usage:

Validator::make(
    [
        'name' => 'Michael Nabil',
    ],
    [
        'name' => [
            'required', 
            Rule::exists('users')->where('id', $this->id)->onlyTrashed(),  
            Rule::unique('users')->where('id', $this->id)->onlyTrashed(),  // Or
        ],
    ]
);

@taylorotwell
taylorotwell merged commit 25c8fed into laravel:9.x Nov 17, 2022
@michaelnabil230
michaelnabil230 deleted the DatabaseRule branch November 17, 2022 14:31
@martinbean

Copy link
Copy Markdown
Contributor

The naming seems off on this for me. “Only trashed” reads to me as if it’ll only check soft-deleted records; not omit soft-deleted (trashed) records when doing the exists/unique checking 😕

@s4muel

s4muel commented Nov 27, 2022

Copy link
Copy Markdown
Contributor

The naming seems off on this for me. “Only trashed” reads to me as if it’ll only check soft-deleted records; not omit soft-deleted (trashed) records when doing the exists/unique checking 😕

@martinbean Well, it reads to you just fine, because it really checks only within soft-deleted items 🤯

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