diff --git a/CHANGELOG.md b/CHANGELOG.md index 27e71cfb..60780670 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to `laravel-data-resource` will be documented in this file. +## 1.0.3 - 2021-11-02 + +- allow ignoring with a closure within a unique rule + ## 1.0.2 - 2021-11-02 - add a `WithData` trait for quicker getting data from objects diff --git a/docs/advanced-usage/validation-attributes.md b/docs/advanced-usage/validation-attributes.md index f08ff1bb..0aec6b19 100644 --- a/docs/advanced-usage/validation-attributes.md +++ b/docs/advanced-usage/validation-attributes.md @@ -850,6 +850,9 @@ public string $value; #[Unique('users', ignore: 5)] public string $value; + +#[Unique('users', ignore: fn() => request()->get('email'))] +public string $value; ``` ### Url diff --git a/docs/as-a-data-transfer-object/request-to-data-object.md b/docs/as-a-data-transfer-object/request-to-data-object.md index ec0206d1..50518ee0 100644 --- a/docs/as-a-data-transfer-object/request-to-data-object.md +++ b/docs/as-a-data-transfer-object/request-to-data-object.md @@ -143,8 +143,8 @@ class SongData extends Data public static function fromRequest(Request $request): static { return new self( - {$request->input('title_of_song')}, - {$request->input('artist_name')} + $request->input('title_of_song'), + $request->input('artist_name') ); } } diff --git a/src/Attributes/Validation/Unique.php b/src/Attributes/Validation/Unique.php index a14cbaa0..2d3037be 100644 --- a/src/Attributes/Validation/Unique.php +++ b/src/Attributes/Validation/Unique.php @@ -13,7 +13,7 @@ public function __construct( private string $table, private ?string $column = 'NULL', private ?string $connection = null, - private ?string $ignore = null, + private string|Closure|null $ignore = null, private ?string $ignoreColumn = null, private bool $withoutTrashed = false, private string $deletedAtColumn = 'deleted_at', @@ -33,7 +33,10 @@ public function getRules(): array } if ($this->ignore) { - $rule->ignore($this->ignore, $this->ignoreColumn); + $rule->ignore( + is_callable($this->ignore) ? ($this->ignore)() : $this->ignore, + $this->ignoreColumn + ); } if ($this->where) { diff --git a/tests/Attributes/Validation/RulesTest.php b/tests/Attributes/Validation/RulesTest.php index f8167a33..e53802d6 100644 --- a/tests/Attributes/Validation/RulesTest.php +++ b/tests/Attributes/Validation/RulesTest.php @@ -979,6 +979,11 @@ public function uniqueAttributesDataProvider(): Generator 'expected' => [(new BaseUnique('users', 'email'))->ignore(5, 'uuid')], ]; + yield [ + 'attribute' => new Unique('users', 'email', ignore: fn () => 5), + 'expected' => [(new BaseUnique('users', 'email'))->ignore(5)], + ]; + $closure = fn (Builder $builder) => $builder; yield [