Skip to content

Commit 25c8fed

Browse files
[9.x] Add a new function onlyTrashed (#44989)
* Add a new function `onlyTrashed` * Add test `onlyTrashed` in `ValidationUniqueRuleTest` * Update DatabaseRule.php Co-authored-by: Taylor Otwell <[email protected]>
1 parent 64f11a3 commit 25c8fed

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

src/Illuminate/Validation/Rules/DatabaseRule.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,19 @@ public function withoutTrashed($deletedAtColumn = 'deleted_at')
182182
return $this;
183183
}
184184

185+
/**
186+
* Only include soft deleted models during the existence check.
187+
*
188+
* @param string $deletedAtColumn
189+
* @return $this
190+
*/
191+
public function onlyTrashed($deletedAtColumn = 'deleted_at')
192+
{
193+
$this->whereNotNull($deletedAtColumn);
194+
195+
return $this;
196+
}
197+
185198
/**
186199
* Register a custom query callback.
187200
*

tests/Validation/ValidationExistsRuleTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,17 @@ public function testItIgnoresSoftDeletes()
234234
$this->assertSame('exists:table,NULL,softdeleted_at,"NULL"', (string) $rule);
235235
}
236236

237+
public function testItOnlyTrashedSoftDeletes()
238+
{
239+
$rule = new Exists('table');
240+
$rule->onlyTrashed();
241+
$this->assertSame('exists:table,NULL,deleted_at,"NOT_NULL"', (string) $rule);
242+
243+
$rule = new Exists('table');
244+
$rule->onlyTrashed('softdeleted_at');
245+
$this->assertSame('exists:table,NULL,softdeleted_at,"NOT_NULL"', (string) $rule);
246+
}
247+
237248
protected function createSchema()
238249
{
239250
$this->schema('default')->create('users', function ($table) {

tests/Validation/ValidationUniqueRuleTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,17 @@ public function testItIgnoresSoftDeletes()
9292
$rule->withoutTrashed('softdeleted_at');
9393
$this->assertSame('unique:table,NULL,NULL,id,softdeleted_at,"NULL"', (string) $rule);
9494
}
95+
96+
public function testItOnlyTrashedSoftDeletes()
97+
{
98+
$rule = new Unique('table');
99+
$rule->onlyTrashed();
100+
$this->assertSame('unique:table,NULL,NULL,id,deleted_at,"NOT_NULL"', (string) $rule);
101+
102+
$rule = new Unique('table');
103+
$rule->onlyTrashed('softdeleted_at');
104+
$this->assertSame('unique:table,NULL,NULL,id,softdeleted_at,"NOT_NULL"', (string) $rule);
105+
}
95106
}
96107

97108
class EloquentModelStub extends Model

0 commit comments

Comments
 (0)