通过laravel中的api更新用户

通过laravel中的api更新用户

本文介绍了通过laravel中的api更新用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 api 更新用户详细信息.

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

        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 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.

这是我在 react-native 中的代码

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)
    navigate('UserAccount')
  }
}

这段代码的错误在哪里?

Where in this codes is the error?

推荐答案

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

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

这篇关于通过laravel中的api更新用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 08:00