Skip to content

Commit 8acdac0

Browse files
[8.x] Add withoutTrashed on Exists rule (#38314)
* add withoutTrashed on Exists rule * Update Exists.php Co-authored-by: Taylor Otwell <[email protected]>
1 parent 54713b3 commit 8acdac0

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/Illuminate/Validation/Rules/Exists.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ class Exists
88
{
99
use Conditionable, DatabaseRule;
1010

11+
/**
12+
* Ignore soft deleted models during the existence check.
13+
*
14+
* @param string $deletedAtColumn
15+
* @return $this
16+
*/
17+
public function withoutTrashed($deletedAtColumn = 'deleted_at')
18+
{
19+
$this->whereNull($deletedAtColumn);
20+
21+
return $this;
22+
}
23+
1124
/**
1225
* Convert the rule to a validation string.
1326
*

tests/Validation/ValidationExistsRuleTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,17 @@ public function testItChoosesValidRecordsUsingWhereNotInAndWhereNotInRulesTogeth
173173
$this->assertFalse($v->passes());
174174
}
175175

176+
public function testItIgnoresSoftDeletes()
177+
{
178+
$rule = new Exists('table');
179+
$rule->withoutTrashed();
180+
$this->assertSame('exists:table,NULL,deleted_at,"NULL"', (string) $rule);
181+
182+
$rule = new Exists('table');
183+
$rule->withoutTrashed('softdeleted_at');
184+
$this->assertSame('exists:table,NULL,softdeleted_at,"NULL"', (string) $rule);
185+
}
186+
176187
protected function createSchema()
177188
{
178189
$this->schema('default')->create('users', function ($table) {

0 commit comments

Comments
 (0)