【问题标题】:How to update user through api in laravellaravel中如何通过api更新用户
【发布时间】:2020-05-28 15:03:01
【问题描述】:
I'm trying to update user through api, and this is the function.

PUT 请求 http://20a11140.ngrok.io/api/userregister/24 例如,ID 为 24 的用户。 我正在使用邮递员进行测试 当我通过编辑的详细信息时,包含名称

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',

        ]);

    }

当我通过编辑的详细信息时,包括姓名和电子邮件,以及所有必需的信息。 当我运行这段代码时,我得到了这个

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

这里有什么问题?

【问题讨论】:

  • 您确定要发出 PUT 请求吗?它编码是否正确,如果是,是什么类型的?显然,数据并没有像您预期的那样到达控制器,所以我建议查看您发送的数据,或者尝试在控制器中调试以捕获实际接收到的数据。
  • 这能回答你的问题吗? Updating a User through the api in laravel

标签: php laravel api laravel-5 eloquent


【解决方案1】:

您在发送数据时出错的地方。

如果您使用 API 中间件,则必须使用 Postman 使用 Body Raw Json 发送数据。

{
    "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\" : \"brn.rajoriya@gmail.com\"\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": {}
}

使用这个邮递员收藏。

【讨论】:

猜你喜欢
  • 2020-05-28
  • 2021-04-01
  • 2019-12-28
  • 1970-01-01
  • 2020-09-02
  • 1970-01-01
  • 1970-01-01
  • 2016-08-04
  • 2021-01-04
相关资源
最近更新 更多