How to check unique key validation on Update

Goutham Mohadas posted 4 months ago

While adding a record we can check a unique key field with unique:<table>,<column> validation but this doesn't seem to work with update, because when some other fields are updated this validation check fails. I found a workaround by comparing the id with a where query, but is there a short/proper way for this?

Cameron replied 4 months ago

Can you add this to your model ?

protected $rules = [
    'Column_name' => 'unique:yourmodel',
Pardeeptech replied 4 months ago

I suggest you to try this code

$emp_id = Request::get('emp_id', $default);

$rules = [
    'emp_id' => 'unique:emp_details,emp_id,NULL,id....'. ..

Let me know if you face problem

Goutham Mohadas replied 4 months ago

I found the solution we can pass id along with check to overcome this problem:


like that.

Thank you @Cameron and @Pardeeptech :-)

Cameron replied 4 months ago

Where/how are you using that snytax? Maybe I've been doing it wrong lol. "unique:<table>,<column>,"$id

Sign in to participate in this thread!

We'd like to thank these amazing companies for supporting us