Support the ongoing development of Laravel.io →
posted 6 years ago
Validation

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?

Last updated 2 years ago.
0

Can you add this to your model ?

protected $rules = [
    'Column_name' => 'unique:yourmodel',
    ..
];
0

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

0

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

"unique:<table>,<column>,"$id

like that.

Thank you @Cameron and @Pardeeptech :-)

0

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

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Goutham Mohadas gmdas Joined 11 Mar 2019

Moderators

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

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.