Cryan
Cryan

Reputation: 23

Laravel 8 Unique form validation ignore

My form edit

<form action="{{ route('admin.akun.update', $akun->id_pegawai) }}" method="POST">
@csrf
@method('patch')
<input type="text" class="form-control" @if (old('nip')) value="{{old('nip')}}" @else value="{{$akun->nip}}" @endif name="nip">

My Route

Route::patch('admin/akun/{id_pegawai}', 'AkunController@update')->name('admin.akun.update');

My Controller

public function update(Request $req, $id_pegawai)
{
    $user = new User;
    $pegawai = new Pegawai;
    $this->validate($req, [
        'nip' => ["required", "unique:users,nip," . $id_pegawai],
    ]);
}

condition: I edit my data, I have a unique field, if I don't change the unique field it will be successfully edited, and if I replace it with the same data there will be an alert

Upvotes: 0

Views: 7732

Answers (2)

Cl&#233;ment Baconnier
Cl&#233;ment Baconnier

Reputation: 6058

You need to specify against which column you want to be unique

 Rule::unique('users', 'nip')->ignore($id_pegawai, 'id_pegawai'),

Upvotes: 4

Yusuf Onur Sari
Yusuf Onur Sari

Reputation: 444

Try it like this:

["required", "unique:users,nip,".$id_pegawai]

Documentatin: https://laravel.com/docs/8.x/validation#rule-unique

Syntax: unique:table,column,except,idColumn

Upvotes: 0

Related Questions