Php Laravel Vue-获得关于json响应的口才
我试图建立一个基本的Laravel Vue评论系统,以便学习两者 现在我试图显示一篇特定文章的所有评论 我的控制器返回json_响应Php Laravel Vue-获得关于json响应的口才,php,json,laravel,vue.js,axios,Php,Json,Laravel,Vue.js,Axios,我试图建立一个基本的Laravel Vue评论系统,以便学习两者 现在我试图显示一篇特定文章的所有评论 我的控制器返回json_响应 public function comments(Post $post) { return response()->json( $post->comments ); } 一切正常,我使用Axios get方法得到以下json响应 [ { "id":1, "post_id":1, "user_
public function comments(Post $post)
{
return response()->json(
$post->comments
);
}
一切正常,我使用Axios get方法得到以下json响应
[
{
"id":1,
"post_id":1,
"user_id":1,
"body":"Post Comment 1",
"created_at":null,
"updated_at":null,
"deleted_at":null
},
{
"id":2,
"post_id":1,
"user_id":1,
"body":"Post comment 2",
"created_at":null,
"updated_at":null,
"deleted_at":null
}
]
我正在用Vue显示所有数据,但我不确定如何才能获得我对评论和用户模型的有力关系
在Blade中,如果我在视图中以数据形式返回响应,我可以显示用户,如下所示:
@foreach($post->comments as $comment)
{{ $comment->user->username }}
@endforeach
如何在json响应中传递user与comments的关系,以从user_id值获取注释的用户?我在Laracasts的帮助下设法解决了这个问题
我必须改变我对这个问题的反应
public function comments(Post $post)
{
return response()->json(
$post->comments->load('user');
);
}