Home » Php » javascript – Updating a User through the api in laravel

javascript – Updating a User through the api in laravel

Posted by: admin February 25, 2020 Leave a comment


I’m trying to update user details through an api.

 public function update(Request $request, $id)
        $validator = Validator::make($request->all(), [
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'string|min:6|confirmed',
            'phone' => 'string|min:6',
            'Age' => 'string',
            'Blood' => 'string',
            'Gender' => 'string',
            'Height' => 'string',
            'Weight' => 'string',
            'record' => 'string'

                return response()->json($validator->errors()->toJson(), 400);

        $doc = User::find($id);

            // Get filename with the extension
            $filenameWithExt = $request->file('picture')->getClientOriginalName();
            // Get just filename
            $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
            // Get just ext
            $extension = $request->file('picture')->getClientOriginalExtension();
            // Filename to store
            $fileNameToStore= $filename.'_'.time().'.'.$extension;
            // Upload Image
            $path = $request->file('picture')->storeAs('public/images', $fileNameToStore);
        } else {
            $fileNameToStore = 'noimage.jpg';

        $doc->name = $request->input('name');
          $doc->email = $request->input('email');
          $doc->phone = $request->input('phone');
            $doc->picture = $fileNameToStore;


        return response()->json([
            'message' => 'Success',



When I run this code, I get this

"{\"name\":[\"The name field is required.\"],\"email\":[\"The email field is required.\"]}"

I noticed that I get this error when I use form-data in post man, if I sent it raw instead, it works.
Then I tried to use it in my react native app, I get the same error as if I used form-data.

This is my code in react-native

const update = dispatch => {

  return async (name, email, phone, picture, Age, Blood, Gender, Height, Weight, id) => {
    const data = new FormData();

    data.append('name', name);
    data.append('email', email);
    data.append('phone', phone);
    data.append('Age', Age);
    data.append('Blood', Blood);
    data.append('Gender', Gender);
    data.append('Height', Height);
    data.append('Weight', Weight);
    data.append("picture", {
      type: 'image/jpg',
      uri: picture,
      name: 'profilepic.jpg'
    const config = {
      method: 'put',
      url: `http://27a50145.ngrok.io/api/userregister/${id}`,
      data: data,
      headers: { 'Content-Type': 'application/json' }

    await axios(config)

Where in this codes is the error?

How to&Answers:

The way you need to change on Frontend.

Use Body instead of Data param.

const config = {
  method: 'put',
  url: `http://27a50145.ngrok.io/api/userregister/${id}`,
  body: JSON.stringify(data),
  headers: { 'Content-Type': 'application/json' }