$uniqueRule = function ($attribute, $value, $fail) use ($id, $columnName) {
if (!empty($value)) {
$count = \DB::table('users')
->where($columnName, $value)
->where('id', '<>', $id)
->count();
if ($count > 0) {
$fail($attribute.' is not unique.');
}
}
};
$rules = [
'password' => 'same:confirm-password',
'email' => ['email', $uniqueRule->bindTo(null, null, $id, 'email')],
'phone' => [$uniqueRule->bindTo(null, null, $id, 'phone')],
'pan_card' => [$uniqueRule->bindTo(null, null, $id, 'pan_card')],
'id_card_num' => [$uniqueRule->bindTo(null, null, $id, 'id_card_num')],
// 'roles' => 'required'
];
$rules = [
'password' => 'same:confirm_password', // Update 'confirm-password' to 'confirm_password'
'email' => 'required|email|unique_except_current_user:users,email,' . $id,
'phone' => 'nullable|unique_except_current_user:users,phone,' . $id,
'pan_card' => 'nullable|unique_except_current_user:users,pan_card,' . $id,
'id_card_num' => 'nullable|unique_except_current_user:users,id_card_num,' . $id,
// 'roles' => 'required'
];
$rules = [
'password' => 'same:confirm-password',
'email' => [
function ($attribute, $value, $fail) use ($id) {
if (!empty($value)) {
$count = \DB::table('users')->where('email', $value)->where('id', '<>', $id)->count();
if ($count > 0) {
$fail($attribute.' is not unique.');
}
}
},
],
'phone' => [
function ($attribute, $value, $fail) use ($id) {
if (!empty($value)) {
$count = \DB::table('users')->where('phone', $value)->where('id', '<>', $id)->count();
if ($count > 0) {
$fail($attribute.' is not unique.');
}
}
},
],
'pan_card' => [
function ($attribute, $value, $fail) use ($id) {
if (!empty($value)) {
$count = \DB::table('users')->where('pan_card', $value)->where('id', '<>', $id)->count();
if ($count > 0) {
$fail($attribute.' is not unique.');
}
}
},
],
'id_card_num' => [
function ($attribute, $value, $fail) use ($id) {
if (!empty($value)) {
$count = \DB::table('users')->where('id_card_num', $value)->where('id', '<>', $id)->count();
if ($count > 0) {
$fail($attribute.' is not unique.');
}
}
},
],
// 'roles' => 'required'
];
$validator = Validator::make($request->all(), $rules);
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter