framework: Soft delete ignored with exists validation rule
I would like to make sure a resource exists using the exists validation rule like normal however I would like to not include soft deleted resources. Since the database will only check against null
it doesn’t seem to work since the rule will use a string. For now I have hacked in…
$extra[$segments[$i]] = $segments[$i + 1] == '{NULL}' ? null : $segments[$i + 1];
into getExtraExistConditions
and have my rule have {NULL}
but obviously that isn’t a good solution, wondering if something can be done about this?
About this issue
- Original URL
- State: closed
- Created 11 years ago
- Comments: 29 (14 by maintainers)
You can pass extra conditions to exists… passing “NULL” will do a whereNull check.
Like:
Do note the difference in argument order for unique and exists, I don’t know for what reason - it seems rather silly and apparently @robclancy is the one who suggested it.
exists:table,column,extra_column,1,extra_column_two,2
unique:table,column,1,extra_column,extra_column_two,2
I even told you the code! What the hell.
exists:table,email,deleted_at,NULL
. No where does it say to doexists:table,email,NULL,deleted_at
. Are you high?No? You are doing it wrong. The docs are fine. You would think column NULL not found is pretty clear on what you are doing wrong. Basically, you have the NULL on the column field, so it is not found.
Nope docs are right. I need more sleep. (if you read the reply I just deleted, I quit life).
You do
exists:table,email,deleted_at,NULL
.This doesn’t work. Throws an error
column NULL not found
You may also use the following construction, for me, it looks clearer:
Thanks, @heroselohim . Your answer really helpful.
For those who want to implement these rule to global scope. Here’s how to do it :
app/Providers/AppServiceProviders.php
.boot()
method.Tips If you want to customize validation message, you should do:
resources/lang/en/validation.php
.exists_soft
to array validation. (I prefer copy fromexists
rule and change it toexists_soft
)Works perfectly;
I know this is a really old topic, but I looked everywhere for something like this with no results. It m8 be of use to someone.
This is the laravel validator for Exists && Not Soft Deleted rule
Yes I have tried the code that is in the docs. I use it in a production environment, hence requesting the feature.
'user_id' => 'exists:users,id,deleted_at,NULL'
will check if there is a user exists at the id and that deleted_at is not null (so they aren’t soft deleted).'user_id' => 'exists:users,id,deleted_at,NULL,type,douche'
will check that a user at id exists with deleted_at null and type == ‘douche’.So for example if @anlutro was the user clearly the type would be douche and it would come back as a pass 😉
For exists:table,email,deleted_at,NULL throws column NULL does not exist. One question, what does id in this example (from the doc) do?
In the above I would like to do
and it works but doesn’t make sense.