如何解决Axios发布表单数据用户ID为已登录用户
我想使用具有当前用户登录ID的axios.post发布我的表单数据。
axios.post('http://localhost:8000/api/add-property/',formData,{
headers:{
'Accept': 'application/json','Content-Type': 'multipart/form-data','Access-Control-Allow-Origin': 'http://http://127.0.0.1:8000','Access-Control-Allow-Credentials': true,}
}
).then(function(response){
console.log(response.data.message)
}).catch(function(error){
console.log(error)
});
请告诉我这些事情吗?
- 添加具有登录用户ID的某些数据的api网址应该是什么?
- 如何获取当前登录的用户ID?
我的后端在laravel中,我的商店功能控制器是这样的:-
public function AddProperty(Request $request,$id) {
$validator = Validator::make($request->all(),[
"agency_name" => "required","agency_location" => "required",// my rest of validations
]
);
if($validator->fails()) {
return response()->json(["status" => "failed","validation_errors" => $validator-
>errors()]);
}
//--------------------get user id---------------------------------------------------
$user = array();
$user = DB::table('users')->where('id',$id)->first();
$user_identification = $user->id;
//----------------------------------------------------------------------------------
$agency_name = $request->input('agency_name');
$agency_location = $request->input('agency_location');
$user_id = $user_identification;
$propertyArray = array(
'agency_name' => "$agency_name",'agency_location' => "$agency_location",'user_id' => $user_identification
);
$property = AddProperty::create($propertyArray);
return "property added succesfully with id:".$property->id;
我添加属性的路线是:-
Route::post("add-property/{id}","AddPropertyController@AddProperty");
当我从邮递员传递ID时,它将数据输入数据库中,我对如何从react获取ID并将其传递到post api中并在我的数据库中输入数据感到困惑。
请帮助我,谢谢!
解决方法
如果您使用的是laravel身份验证,则只需致电
$user = Auth::user();
检索经过身份验证的用户
如果您不使用laravel身份验证,那么这取决于您如何实现
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。