Veuillez m'aider à trouver le problème.
Parfois (pas toujours), le code suivant insère deux enregistrements dans la base de données (dans la table des utilisateurs ainsi que dans la table des profils), mais je vérifie avant l'insertion que le "mobile_no" existe déjà ou non afin de créer des enregistrements uniques basés sur le numéro de mobile.
static function postData($data) {
try {
if (isset($data['number'])) {
//exist
$exist = Profile::where('mobile_no', '=', $data['number'])->get();
//print_r($exist);
if (count($exist) > 0 ) {
$user = User::find($exist[0]['user_id']);
if (isset($data['last_name'])) {
$user->first_name = $data['first_name'];
}
if (isset($data['last_name'])) {
$user->last_name = $data['last_name'];
}
if (isset($data['email'])) {
$user->email = $data['email'];
}
$user->save();
$proid = $exist[0]['user_id'];
$profile_result = Profile::find($proid);
if (isset($data['number'])) {
$profile_result->mobile_no = $data['number'];
}
if (isset($data['birthday'])) {
$profile_result->dob = $data['birthday'];
}
$profile_result->save();
return $exist[0]['user_id'];
}else{
$user = new User();
if (isset($data['first_name'])) {
$user->first_name = $data['first_name'];
}
if (isset($data['last_name'])) {
$user->last_name = $data['last_name'];
}
$user->save();
$id = $user->id;
$profile = new Profile();
$profile->user_id = $id;
if (isset($data['mobile_number'])) {
$profile->mobile_no = $data['number'];
}
if (isset($data['birthday'])) {
$profile->dob = $data['birthday'];
}
$profile->save();
$proid = $profile->user_id;
$profile_result = $profile::where('user_id', '=', $proid)->get();
$user_result = $user::where('id', '=', $id)->get();
$output = array();
return (string) $id;
}
}
} catch (Exception $ex)
{
return $ex;
}
}