Php 如何在laravel中通过api更新用户
以id为24的用户的请求为例。 我在用邮递员做测试 当我传递已编辑的详细信息时,包含名称Php 如何在laravel中通过api更新用户,php,laravel,api,laravel-5,eloquent,Php,Laravel,Api,Laravel 5,Eloquent,以id为24的用户的请求为例。 我在用邮递员做测试 当我传递已编辑的详细信息时,包含名称 I'm trying to update user through api, and this is the function. 当我传递经过编辑的详细信息时,包括姓名、电子邮件以及所有必需的信息。 当我运行这个代码时,我得到了这个 public function update(Request $request, $id) { $validator = Validator::make($r
I'm trying to update user through api, and this is the function.
当我传递经过编辑的详细信息时,包括姓名、电子邮件以及所有必需的信息。
当我运行这个代码时,我得到了这个
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',
]);
}
这里的问题是什么?在某些地方,您发送数据时出错了 如果您使用的是API中间件,那么您必须使用Postman使用Body原始Json发送数据
"{\"name\":[\"The name field is required.\"],\"email\":[\"The email field is required.\"]}"
使用此邮递员收藏。您确定要发出PUT请求吗?它的编码是否正确,如果正确,是什么类型?很明显,数据没有像您预期的那样到达控制器,因此我建议查看您正在发送的数据,或者尝试在控制器中调试以捕获实际接收到的数据。这是否回答了您的问题?当我使用身体生的时候它就起作用了。但它在我的react原生项目中不起作用。这是你命中API的方式吗?
{
"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": {}
}