Home » Php » php – How to update user through api in laravel

php – How to update user through api in laravel

Posted by: admin February 25, 2020 Leave a comment

Questions:
I'm trying to update user through api, and this is the function.

PUT request http://20a11140.ngrok.io/api/userregister/24 for user with id 24 for example.
I’m using postman for testing
When I pass the edited details, with name inclusive

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'
        ]);

    if($validator->fails()){
            return response()->json($validator->errors()->toJson(), 400);
}

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

        if($request->hasFile('picture')){
            // 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');
          if($request->hasFile('picture')){
            $doc->picture = $fileNameToStore;
            }



           $doc->save();

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

        ]);

    }

When I pass the edited details, with name inclusive and email, and every required info.
When I run this code, I get this

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

What is the issue here?

How to&Answers:

Somewhere you are wrong with sending data.

If you are using API middlewares then you will have to send data with Body Raw Json using Postman.

{
    "info": {
        "_postman_id": "9bc0d91b-83e7-4ccf-a561-ac191c21e869",
        "name": "Laravel",
        "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
    },
    "item": [
        {
            "name": "http://20a11140.ngrok.io/api/userregister/24",
            "request": {
                "method": "PUT",
                "header": [
                    {
                        "key": "Content-Type",
                        "name": "Content-Type",
                        "value": "application/json",
                        "type": "text"
                    }
                ],
                "body": {
                    "mode": "raw",
                    "raw": "{\n\t\"name\" : \"BHaskar Rajoriya\",\n\t\"email\" : \"[email protected]\"\n}",
                    "options": {
                        "raw": {
                            "language": "json"
                        }
                    }
                },
                "url": {
                    "raw": "http://20a11140.ngrok.io/api/userregister/24",
                    "protocol": "http",
                    "host": [
                        "20a11140",
                        "ngrok",
                        "io"
                    ],
                    "path": [
                        "api",
                        "userregister",
                        "24"
                    ]
                }
            },
            "response": []
        }
    ],
    "protocolProfileBehavior": {}
}

Use this postman collection.