1
我想學習Vue.js,它使用Laravel來構建API構建。這個想法很簡單,用戶可以發表帖子,帖子可以發表評論。我可以在laravel中獲得關係,但我無法弄清楚如何使用Vue.js返回帖子或評論的作者姓名。Laravel API - Vue.js返回帖子用戶
當使用我用這樣的事情在foreach循環返回作者姓名刀片模板引擎:
{{ $post->user->name }}
當我回到崗位低谷的API,我得到沒有得到任何用戶信息,用戶標識除外。我如何獲得屬於這篇文章的用戶信息?
{
"message": "Success",
"data": [
{
"id": 1,
"body": "test body 1",
"user_id": "1",
"created_at": "2016-09-16 10:22:57",
"updated_at": "2016-09-16 10:22:57"
}
]
}
<script>
export default {
/*
* The component's data.
*/
data() {
return {
posts: [],
};
},
/**
* Prepare the component.
*/
ready() {
this.getPosts();
},
methods: {
/**
* Get Posts.
*/
getPosts: function() {
this.$http.get('/api/wall/posts')
.then(response => {
this.posts = response.data.posts;
});
}
}
}
</script>
public function getPosts($id = null)
{
if (!$id){
$data = Post::all();
$message = 'Success';
$code = 200;
} else {
$data = Post::FindOrFail($id);
$message = 'Success';
$code = 200;
}
return Response::json(['message' => $message, 'data' => $data], $code);
}
Brainfart ...感謝您的回答,我應該想到這一點...... –