i'm trying to update a user's profile( social web app), i'm working with React as a frontend and laravel as a backend. the data includes an image, so i sent it using FormData, i'm doing a small validation in backend to check if data is sent to the backend, but i found that axios is not sending formData. Here is my function that runs after submit:
const editProfile = (e) => {
e.preventDefault()
setLoading(true)
const formData = new FormData();
formData.append('avatar', pic.avatar)
formData.append('website', profile.website)
formData.append('addresse', profile.addresse)
formData.append('bio', profile.bio)
formData.append('phone', profile.phone)
formData.append('gender', profile.gender)
formData.append('full_name', auth.full_name)
formData.append('user_name', auth.user_name)
formData.append('email', auth.email)
console.log(formData)
axios.put(`api/profile/update/${id}`, formData).then((res) => {
if(res.data.success) {
console.log('success')
history.push(`/profile/${id}`)
}else {
console.log(res.data.message)
setLoading(false)
}
})
}
I only made gender is required in laravel:
public function update(Request $request, $id)
{
$profile = Profile::find($id);
if(is_null($profile)) {
return response()->json([
'success' => false,
'message' => 'Profile can not be found!'
]);
}else {
$validate = Validator::make($request->all(),[
'gender' => 'required'
]);
if($validate->fails()) {
return response()->json([
'success' => false,
'message' => $validate->messages()
]);
}else {...
As its mentioned in react logic, i'm doing console.log to check errors if the response is failed, and i;m getting gender field is required, i tried to do a console.log(profile.gender) before sending data to axios and i could see the value normaly, So can anyone help me please?
ddoutput. - Fer Toasted