diff --git a/app/Http/Requests/DebitureRequest.php b/app/Http/Requests/DebitureRequest.php index 7f83fbb..40b608e 100644 --- a/app/Http/Requests/DebitureRequest.php +++ b/app/Http/Requests/DebitureRequest.php @@ -4,6 +4,7 @@ use Illuminate\Foundation\Http\FormRequest; use Modules\Lpj\Rules\UniqueCifExceptZero; + use Modules\Lpj\Rules\UniqueExcept; class DebitureRequest extends FormRequest { diff --git a/app/Rules/UniqueCifExceptZero.php b/app/Rules/UniqueCifExceptZero.php index c8173d1..03accd3 100644 --- a/app/Rules/UniqueCifExceptZero.php +++ b/app/Rules/UniqueCifExceptZero.php @@ -1,25 +1,32 @@ id = $id; - } + protected $id; - public function validate($attribute, $value, $fail): void - { - if (Debiture::where($attribute, $value) - ->where('id', '!=', $this->id) - ->where($attribute, '!=', '0000000000') - ->exists()) { - $fail('The :attribute field must be uniquse.'.$this->id); + public function __construct($id = null) + { + $this->id = $id; + } + + /** + * Run the validation rule. + */ + public function validate(string $attribute, mixed $value, Closure $fail) + : void { + if ($value !== '0000000000' && $value !== null && Debiture::query()->where($attribute, $value)->when( + $this->id, + function ($query) { + $query->where('id', '!=', $this->id); + }, + )->exists()) { + $fail('The :attribute field must be unique.'); + } } } -} diff --git a/database/migrations/2024_11_01_023117_update_debitures_table.php b/database/migrations/2024_11_01_023117_update_debitures_table.php new file mode 100644 index 0000000..cadf6b0 --- /dev/null +++ b/database/migrations/2024_11_01_023117_update_debitures_table.php @@ -0,0 +1,28 @@ +dropUnique(['cif']); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('debitures', function (Blueprint $table) { + $table->string('cif')->unique()->change(); + }); + } +};