Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel Vue-获得关于json响应的口才_Php_Json_Laravel_Vue.js_Axios - Fatal编程技术网

Php 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_

我试图建立一个基本的Laravel Vue评论系统,以便学习两者

现在我试图显示一篇特定文章的所有评论

我的控制器返回json_响应

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');
    );
}