我正在尝试使用axios从Vuejs前端向后端的Laravel发送post请求。
const data = {
file: {id}
}
axios.post('api/documents/remove', data).then((response) => {
console.log(response.data);
}
}).catch(err => { console.log(err.response.data); })在laravel中,我像这样打印请求,
public function removeFile(Request $request)
{
dd($request);
}这是我的输出。
Illuminate\Http\Request {#51
#json: Symfony\Component\HttpFoundation\ParameterBag {#43
#parameters: array:8 [
"file" => array:1 [
"id" => 61
]
}发布于 2020-12-04 15:59:40
PHP的"arrays“不是真正的数组,它们是有序的映射。这就是您的输出所显示的,请求包含参数,这是一个有序映射,而file参数是一个带有id => 61的有序映射。在JavaScript中,它可能是一个对象(有时也可能是Map );在许多其他语言中,它可能是某种映射或字典。PHP称它们为数组。
无论哪种方式,如果您访问file参数(我不是“做”Laravel,所以我不知道您是如何做到的),您可以像这样获得id:$file[id]。看一下Laravel docs,看起来应该是:
$file = $request->input('file');
$id = $file['id'];发布于 2020-12-04 16:03:15
您可以通过以下方式获取$request的数据:
$file = $request->input('file');
$file['id'];
//Or
$fileId = $request->input('file.id');发布于 2021-09-18 20:10:00
Request $request传递的是数组而不是object实例,因此,您只能像这样访问:
$request->input('file')['id']或
$request->file['id']或
$request->input('file.id')或者,仅文件字段
$request-all()[0]['file']['id']https://stackoverflow.com/questions/65139808
复制相似问题