why all fields are always empty in form-data? if i change raw/x-www-form-urlencoded everything works fine.
file AuthController.php :
user();
try {
$user->update($request->only(['name', 'email', 'password']));
if ($request->has('password')) {
$user->password = Hash::make($request->password);
$user->save();
}
return $this->successResponse(
$user->only([
'id',
'name',
'email',
'created_at',
'updated_at',
'photo',
]),
'User profile updated successfully',
Response::HTTP_OK
);
} catch (\Exception $e) {
Log::error('Failed to update user profile', [
'user_id' => $user->id,
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
]);
return $this->errorResponse(
'Failed to update profile. Please try again later.',
Response::HTTP_INTERNAL_SERVER_ERROR
);
}
}
}
file ProfileRequest.php :
'required|string|min:2|max:50',
'email' => 'required|email|unique:users,email,' . auth()->user()->id,
'password' => 'required|string|min:6|confirmed',
];
}
public function messages()
{
return [
'name.required' => 'The name field is required.',
'name.min' => 'The name must be at least :min characters.',
'name.max' => 'The name may not be greater than :max characters.',
'email.required' => 'The email field is required.',
'email.email' => 'The email must be a valid email address.',
'email.unique' => 'This email has already been taken.',
'password.min' => 'The password must be at least :min characters.',
'password.confirmed' => 'The password confirmation does not match.',
];
}
}
file api.php :
get('/profile', [AuthController::class, 'profile']);
Route::middleware('auth:sanctum')->put('/profile', [AuthController::class, 'updateProfile']);
body : raw
enter image description here
body : x-www-form-urlencoded
enter image description here
body : form-data
enter image description here
I tried using form-data because later there will be a photo field that contains image files. But before I added it, even my current code has an error on form-data
I just want to add a photo field that contains image files