I have a table that defines user connections like facebook friend. It has fields like -
id------------ sender_id ---------- receiver_id ---------------- status
Now, I want to validate if same data exists between 2 columns. Means,
1 ------------- 11 ---------- 22 -------- status if this row exists, then
1 ------------- 11 ---------- 22 -------- status this shouldn't be there. also
1 ------------- 22 ---------- 11 -------- status this shouldn't be there.
Here, the sender_id is the auth user id.
I am trying to validate the receiver_id
$thisUser = auth()->user();
$validator = Validator::make($request->all(), [
'receiver_id' => [
'required',
'exists:users,id',
Rule::unique('user_connections')->where('sender_id', $thisUser->id),
function($attribute, $value, $fail) use($thisUser, $request) {
if($thisUser->id == $request->input('receiver_id')) {
return $fail('You cannot send request to own!');
}
},
],
]);
question from:
https://stackoverflow.com/questions/65872327/laravel-unique-validation-on-multiple-column 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…