0👍
✅
as I spent too much time on this trivial problem, I’d like to share my final solution made with the help of Hardik. Thanks again !
$bIsAvatar = true;
$user = new User;
$user_email = $request->email;
$rules = [
'email' => 'required|email|min:6',
'username' => 'required|string|min:5',
'zip' => 'string|min:1|max:5',
'avatar' => 'image|mimes:jpeg,png,jpg,gif|max:10000'
];
$validation = Validator::make(Input::all(), $rules);
if ($validation->fails()) {
return response()->json([
'Error' => 'User : ' . $user_email ,
'Champs invalides : ' . $validation->messages()
], Response::HTTP_BAD_REQUEST);
}
$user = User::findByEmail( $user_email );
if (!$user || $user->isBanned() || !$user->is_activated) {
return response()->json(
['Error' => 'User non trouvé ou non autorisé : ' . $user_email ], Response::HTTP_NOT_FOUND
);
}
$user->name = $request->name;
$user->surname = $request->surname;
$user->email = $request->email;
$user->street_addr = $request->street_addr;
$user->city = $request->city;
$user->zip = $request->zip;
$user->phone = $request->phone;
$user->mobile = $request->mobile;
$user->about_me = $request->about_me;
if ( $request->avatar == null || empty($request->avatar) ) $bIsAvatar = false;
else {
$user->avatar = Input::file('avatar');
$user->avatar->is_public = false;
}
$user->save();
if ($bIsAvatar) {
$avatar_path = $user->avatar->getPath();
//$resultat = "User " . $user->email . " : profil mis à jour avec l'avatar=" . $avatar_path;
$resultat = "Profil mis à jour avec avatar" . $user;
} else
//$resultat = "User : " . $user->email . " : profil mis à jour sans avatar";
$resultat = "Profil mis à jour sans avatar" . $user;
return response()->json($resultat);
});
Source:stackexchange.com