【问题标题】:Updating a User through the api in laravel通过 laravel 中的 api 更新用户
【发布时间】:2020-05-28 12:04:41
【问题描述】:

我正在尝试通过 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'
        ]);

        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.\"]}"

我注意到当我在邮递员中使用表单数据时出现此错误,如果我将其发送为原始数据,则它可以工作。 然后我尝试在我的 react native 应用程序中使用它,我得到了和我使用 form-data 一样的错误。

这是我在 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)
    navigate('UserAccount')
  }
}

这段代码哪里出错了?

【问题讨论】:

  • 就像姓名和电子邮件的值为空一样,请确保您的电子邮件和姓名值不为空并正确发送到后端。也许您想通过dd($request->all() 检查您收到的所有值
  • 值不为空

标签: javascript php laravel api react-native


【解决方案1】:

您需要在前端更改的方式。

使用正文而不是数据参数。

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

【讨论】:

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