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 Illuminate\Foundation\Http\FormRequest;
|
||||||
use Modules\Lpj\Rules\UniqueCifExceptZero;
|
use Modules\Lpj\Rules\UniqueCifExceptZero;
|
||||||
|
use Modules\Lpj\Rules\UniqueExcept;
|
||||||
|
|
||||||
class DebitureRequest extends FormRequest
|
class DebitureRequest extends FormRequest
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -8,18 +8,25 @@ use Modules\Lpj\Models\Debiture;
|
|||||||
|
|
||||||
class UniqueCifExceptZero implements ValidationRule
|
class UniqueCifExceptZero implements ValidationRule
|
||||||
{
|
{
|
||||||
|
protected $id;
|
||||||
|
|
||||||
public function __construct($id = null)
|
public function __construct($id = null)
|
||||||
{
|
{
|
||||||
$this->id = $id;
|
$this->id = $id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function validate($attribute, $value, $fail): void
|
/**
|
||||||
{
|
* Run the validation rule.
|
||||||
if (Debiture::where($attribute, $value)
|
*/
|
||||||
->where('id', '!=', $this->id)
|
public function validate(string $attribute, mixed $value, Closure $fail)
|
||||||
->where($attribute, '!=', '0000000000')
|
: void {
|
||||||
->exists()) {
|
if ($value !== '0000000000' && $value !== null && Debiture::query()->where($attribute, $value)->when(
|
||||||
$fail('The :attribute field must be uniquse.'.$this->id);
|
$this->id,
|
||||||
|
function ($query) {
|
||||||
|
$query->where('id', '!=', $this->id);
|
||||||
|
},
|
||||||
|
)->exists()) {
|
||||||
|
$fail('The :attribute field must be unique.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::table('debitures', function (Blueprint $table) {
|
||||||
|
$table->dropUnique(['cif']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::table('debitures', function (Blueprint $table) {
|
||||||
|
$table->string('cif')->unique()->change();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user