Skip to content

Commit 0e489a6

Browse files
Merge pull request #17 from spatie/request-data
Request data
2 parents e14ce17 + e8a0204 commit 0e489a6

File tree

5 files changed

+19
-4
lines changed

5 files changed

+19
-4
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to `laravel-data-resource` will be documented in this file.
44

5+
## 1.0.3 - 2021-11-02
6+
7+
- allow ignoring with a closure within a unique rule
8+
59
## 1.0.2 - 2021-11-02
610

711
- add a `WithData` trait for quicker getting data from objects

docs/advanced-usage/validation-attributes.md

+3
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,9 @@ public string $value;
850850

851851
#[Unique('users', ignore: 5)]
852852
public string $value;
853+
854+
#[Unique('users', ignore: fn() => request()->get('email'))]
855+
public string $value;
853856
```
854857

855858
### Url

docs/as-a-data-transfer-object/request-to-data-object.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ class SongData extends Data
143143
public static function fromRequest(Request $request): static
144144
{
145145
return new self(
146-
{$request->input('title_of_song')},
147-
{$request->input('artist_name')}
146+
$request->input('title_of_song'),
147+
$request->input('artist_name')
148148
);
149149
}
150150
}

src/Attributes/Validation/Unique.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function __construct(
1313
private string $table,
1414
private ?string $column = 'NULL',
1515
private ?string $connection = null,
16-
private ?string $ignore = null,
16+
private string|Closure|null $ignore = null,
1717
private ?string $ignoreColumn = null,
1818
private bool $withoutTrashed = false,
1919
private string $deletedAtColumn = 'deleted_at',
@@ -33,7 +33,10 @@ public function getRules(): array
3333
}
3434

3535
if ($this->ignore) {
36-
$rule->ignore($this->ignore, $this->ignoreColumn);
36+
$rule->ignore(
37+
is_callable($this->ignore) ? ($this->ignore)() : $this->ignore,
38+
$this->ignoreColumn
39+
);
3740
}
3841

3942
if ($this->where) {

tests/Attributes/Validation/RulesTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,11 @@ public function uniqueAttributesDataProvider(): Generator
979979
'expected' => [(new BaseUnique('users', 'email'))->ignore(5, 'uuid')],
980980
];
981981

982+
yield [
983+
'attribute' => new Unique('users', 'email', ignore: fn () => 5),
984+
'expected' => [(new BaseUnique('users', 'email'))->ignore(5)],
985+
];
986+
982987
$closure = fn (Builder $builder) => $builder;
983988

984989
yield [

0 commit comments

Comments
 (0)