Drop unique constraint on 'cif' in debitures table
Remove the unique constraint on the 'cif' field in the debitures table and refactor the `UniqueCifExceptZero` validation rule to handle the uniqueness check more robustly. Also, add an unused import statement for `UniqueExcept` in `DebitureRequest`.
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Modules\Lpj\Rules\UniqueCifExceptZero;
|
||||
use Modules\Lpj\Rules\UniqueExcept;
|
||||
|
||||
class DebitureRequest extends FormRequest
|
||||
{
|
||||
|
||||
@@ -1,25 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Lpj\Rules;
|
||||
namespace Modules\Lpj\Rules;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Modules\Lpj\Models\Debiture;
|
||||
use Closure;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Modules\Lpj\Models\Debiture;
|
||||
|
||||
class UniqueCifExceptZero implements ValidationRule
|
||||
{
|
||||
public function __construct($id = null)
|
||||
class UniqueCifExceptZero implements ValidationRule
|
||||
{
|
||||
$this->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.');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user