'How to validate email while updating in laravel
I have 2 tables named area_managers and shopkeepers. I want to validate email from area_managers while updating in a way so that it doesn't match with the email of shopkeepers and area_managers table. I tried the following way but it doesn't even allow the existed email of the respective user.
public function update(Request $request, AreaManager $areaManager)
{
$request->validate([
'email'=>'required|email|unique:shopkeepers,email|unique:area_managers,email,'.$areaManager->id,
]);
$areaManager-> email = $request->email;
$areaManager->update();
return redirect()->route('area_manager.edit',$areaManager->id);
}
Solution 1:[1]
A quick check could be something like this:
$checkAreaManagers = DB::table('area_managers')->where('email', '=', $request->email)->first();
$checkShopkeepers = DB::table('shopkeepers')->where('email', '=', $request->email)->first();
if ($checkAreaManagers === null || $checkShopkeepers === null) {
// Proceed with the update
$areaManager->email = $request->email;
$areaManager->save();
}
if ($checkAreaManagers !== null || $checkShopkeepers !== null) {
// Cancel the update (Your logic here)
dd('Duplicate found');
}
Solution 2:[2]
you should make a role with this command:
php artisan make:rule rolename
then in passes method set your query and then use in validator
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | fufubrocat |
| Solution 2 | Amir Khaledian |
