我想使用带有当前用户登录 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 url 应该是什么?
- 如何获取当前登录的用户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 时,它会将数据输入到数据库中,我对如何从反应中获取 id 并将其传递到 post api 并将数据输入到我的数据库中感到困惑。
请有人帮助我,并在此先感谢