-
Notifications
You must be signed in to change notification settings - Fork 10.7k
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
Soft delete ignored with exists validation rule #1820
Comments
Maybe a less hacky solution is to use: if( $this->db->connection($this->connection)->getSchemaBuilder()->hasColumn($collection, 'deleted_at') )
{
$query->whereNull('deleted_at');
} In Illuminate\Validation\DatabasePresenceVerifier::getCount() and getMultiCount() methods. Disclaimer: Haven't really tried it. I'll try it later and maybe make a PR. Don't know how to go around when the deleted_at column is renamed tho. |
Yeah that would work but would force it so it is always checking the ones that aren't soft deleted, sometimes people might want to check against them too. However it gives me an idea of a better solution, in each method there is... foreach ($extra as $key => $extraValue)
{
$query->where($key, $extraValue);
} Perhaps this could be changed to (another hacky method)... foreach ($extra as $key => $extraValue)
{
if ($key == 'deleted_at' and empty($extraValue))
{
$query->whereNull($key);
continue;
}
$query->where($key, $extraValue);
} The main issue really is that there is no way to imply None of these methods are good enough to get in, they are all hacky and have flaws. But hopefully something can be done. |
Maybe passing an extra parameter to the rule, something like: exists:table,column,include_deleted |
You can pass extra conditions to exists... passing "NULL" will do a whereNull check. Like:
|
Thanks. |
This doesn't work. Throws an error |
Because you are doing it wrong. |
so does this need to be changed on the docs as well? http://laravel.com/docs/validation#rule-exists |
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. |
so if I wanted to check |
Nope docs are right. I need more sleep. (if you read the reply I just deleted, I quit life). You do |
For
In the above I would like to do
and it works but doesn't make sense. |
I even told you the code! What the hell. |
Sure someone is high! |
Yes I have tried the code that is in the docs. I use it in a production environment, hence requesting the feature.
So for example if @anlutro was the user clearly the type would be douche and it would come back as a pass ;) |
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.
|
FUUUU: I PR'd it exactly the same: #1263 |
In the docs example. First is table, second is column, third is excluded value, fourth is the excluded column, then the following pairs are your where conditions. So in this case fifth is the where column and sixth the where value. The NULL in this case is because there is on exclusion going on. |
Wish you explained as clear as this before and I wish docs was this clear. |
Wish @anlutro never pissed me off on IRC and made me rage on issues ;) Blame him. I'll PR a change to the docs. |
I understand that this topic may be outdated, and some participants may no longer be active, but I believe it would make sense to set deleted_at to NULL by default. While it's possible to encounter an SQL error if you attempt to check whether the deleted_at column exists, you could instead check if the model uses trait, by doing so, you could determine if the SoftDeletes trait is being used. This would prevent users from falling into a trap, as I did. Initially, I assumed that adding the SoftDeletes trait would provide me with the necessary functionality. However, I later discovered that validation was broken in multiple areas when I attempted to run the same code twice. Unfortunately, I only discovered this issue after it was too late. |
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
|
I am going crazy with this, I tried:
|
@frankvice are you sure about
did the trick for me. |
Works perfectly; Rule::unique('users')
->ignore($userId)
->whereNull('deleted_at'), |
Thanks, @heroselohim . Your answer really helpful. For those who want to implement these rule to global scope. Here's how to do it :
Tips
|
Rule::unique("table", "column")->whereNull("deleted_at") |
You may also use the following construction, for me, it looks clearer:
|
thx a lot it works perfectly |
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...into
getExtraExistConditions
and have my rule have{NULL}
but obviously that isn't a good solution, wondering if something can be done about this?The text was updated successfully, but these errors were encountered: