Axios|VueJS|Laravel|Post请求无法访问对象



所以我试图将我的post数据发送到我的控制器,然后通过对象访问它。它肯定会传递数据,但由于某种原因,它不允许我访问该对象中的任何项目,这真的很奇怪。

这是我在vue组件中的帖子请求。

axios.post('/users', {user: this.user})
.then(response => {
console.log(response)
})
.catch(error => {
console.log(error.response)
});

这是我的控制器中的内容,正如我所说,我确实得到了用户信息,但我无法访问它,比如做$user->id,但当我只做$user时,我可以看到包括id在内的用户数据。

//Here we get all the users info from the Axios Vue.
$user = $request->get('user');
return response()->json([
'status' => 'success',
'msg'    => $user->id,
], 201);

我得到的错误是一个php错误,因为它在网络响应区域下,这就是错误消息所说的。

"message": "Trying to get property 'id' of non-object"

但当我在控制器中执行此操作时。

//Here we get all the users info from the Axios Vue.
$user = $request->get('user');
return response()->json([
'status' => 'success',
'msg'    => $user,
], 201);

它返回一个201并在消息中显示。

{"id":1,"email":"test@test.com","full_name":"John Doe","first_name":"John","last_name":"Doe","is_active":1,"overide_login":1,"skill_id":5,"phone":null,"shifts_id":0,"bilingual":0,"full_time":0}

我的对象格式是错误的还是什么?。。。如果有人能帮忙就太好了,因为我真的不明白为什么这不起作用。

请注意,所有内容都在VUEJS/AXIOS和LARAVEL的最新版本中。

您必须首先对其进行解码,请将其添加到控制器中。

$input = ($request->all() == null ? json_decode($request->getContent(), true) : $request->all());
echo $input['user']['id']

希望这对你有用。

最新更新