Php Laravel 5:在用户配置文件中显示此评论所属的帖子标题

Php Laravel 5:在用户配置文件中显示此评论所属的帖子标题,php,mysql,laravel,laravel-5,Php,Mysql,Laravel,Laravel 5,我试图在他的个人资料页面上显示用户评论所属的帖子标题 这就是我在用户个人资料上所做的 public function show($user, Post $post) { $user = User::whereName($user)->with('posts.votes')->with('comments')->firstOrFail(); $comments = $user->comments; $linkKarma = User::find($u

我试图在他的个人资料页面上显示用户评论所属的帖子标题

这就是我在用户个人资料上所做的

public function show($user, Post $post)
{
    $user = User::whereName($user)->with('posts.votes')->with('comments')->firstOrFail();
    $comments = $user->comments;
    $linkKarma = User::find($user->id)->votes()->sum('value');
    $commentKarma = User::find($user->id)->commentvotes()->sum('value');
    $total_comments = $user->comments->count();

    $isModerator = false;

    return view('user/profile')->with('user', $user)
                                ->with('linkKarma', $linkKarma)
                                ->with('commentKarma', $commentKarma)
                                ->with('comments', $comments)
                                ->with('total_comments', $total_comments)
                                ->with('isModerator', $isModerator);
}
我基本上得到了他所有的东西,从
帖子
投票
评论
——但是,我无法在用户配置文件页面上显示这些评论所属的帖子

我可以在
foreach()
中使用
$comment->post\u id
,但这只能获取post id,但我不能使用
$comment->post->title
尝试获取无对象的属性时会出错

我该怎么做

我的数据库

posts: id, title, user_id, subreddit_id...
comments: id, comment, user_id, post_id...
user: id, name, email...
Post
Model

public function user() {
    return $this->belongsTo('App\User');
}

public function subreddit() {
    return $this->belongsTo('App\Subreddit');
}

public function votes() {
    return $this->hasMany('App\Vote');
}

public function moderators() {
    return $this->hasMany('App\Moderator');
}

public function comments() {
    return $this->hasMany('App\Comment');
}
public function posts() {
    return $this->belongsTo('App\Post');
}

public function user() {
    return $this->belongsTo('App\User');
}

public function commentvotes() {
    return $this->hasMany('App\CommentVote');
}

public function subreddit() {
    return $this->belongsTo('App\Subreddit');
}
public function subreddit() {
    return $this->hasMany('App\Subreddit');
}

public function posts() {
    return $this->hasMany('App\Post');
}

public function votes() {
    return $this->hasManyThrough('App\Vote','App\Post');
}

public function commentvotes() {
    return $this->hasManyThrough('App\CommentVote','App\Comment');
}

public function moderators() {
    return $this->hasMany('App\Moderator');
}

public function comments() {
    return $this->hasMany('App\Comment');
}
注释
型号

public function user() {
    return $this->belongsTo('App\User');
}

public function subreddit() {
    return $this->belongsTo('App\Subreddit');
}

public function votes() {
    return $this->hasMany('App\Vote');
}

public function moderators() {
    return $this->hasMany('App\Moderator');
}

public function comments() {
    return $this->hasMany('App\Comment');
}
public function posts() {
    return $this->belongsTo('App\Post');
}

public function user() {
    return $this->belongsTo('App\User');
}

public function commentvotes() {
    return $this->hasMany('App\CommentVote');
}

public function subreddit() {
    return $this->belongsTo('App\Subreddit');
}
public function subreddit() {
    return $this->hasMany('App\Subreddit');
}

public function posts() {
    return $this->hasMany('App\Post');
}

public function votes() {
    return $this->hasManyThrough('App\Vote','App\Post');
}

public function commentvotes() {
    return $this->hasManyThrough('App\CommentVote','App\Comment');
}

public function moderators() {
    return $this->hasMany('App\Moderator');
}

public function comments() {
    return $this->hasMany('App\Comment');
}
用户
型号

public function user() {
    return $this->belongsTo('App\User');
}

public function subreddit() {
    return $this->belongsTo('App\Subreddit');
}

public function votes() {
    return $this->hasMany('App\Vote');
}

public function moderators() {
    return $this->hasMany('App\Moderator');
}

public function comments() {
    return $this->hasMany('App\Comment');
}
public function posts() {
    return $this->belongsTo('App\Post');
}

public function user() {
    return $this->belongsTo('App\User');
}

public function commentvotes() {
    return $this->hasMany('App\CommentVote');
}

public function subreddit() {
    return $this->belongsTo('App\Subreddit');
}
public function subreddit() {
    return $this->hasMany('App\Subreddit');
}

public function posts() {
    return $this->hasMany('App\Post');
}

public function votes() {
    return $this->hasManyThrough('App\Vote','App\Post');
}

public function commentvotes() {
    return $this->hasManyThrough('App\CommentVote','App\Comment');
}

public function moderators() {
    return $this->hasMany('App\Moderator');
}

public function comments() {
    return $this->hasMany('App\Comment');
}
试试这个:

$comment->posts->title
(注意它是“posts”而不是“post”)

您的注释模型具有函数
posts
,因此当您从注释访问它时,您可以使用该函数名访问它,如上所示

我会将帖子重命名为post,因为评论属于一个帖子,而不是多个帖子

也改变

$user = User::whereName($user)->with('posts.votes')->with('comments')->firstOrFail();
将来


这使得
尝试获取非对象的属性
您可以验证注释模型中的函数名是否为
posts
,以及在视图文件中的foreach循环中,您是否通过
$Comment->posts->title
访问它?另外,当您添加或打印$comment时会输出什么?我无法访问
$comment->posts->title
,这就是问题所在。评论没有与帖子链接,因为我已经迫不及待地加载
帖子。投票将获得用户所有评论的集合。尝试更改
$user=user::whereName($user)->with('posts.voates')->with('Comments')->firstOrFail()
将成为
$user=user::whereName($user)->with('posts.vows')->with('comments.posts')->firstOrFail()请提供用于访问用户评论文章标题的代码。还要验证您是否确实有一篇帖子,其id与评论记录中的帖子id相匹配。确保您的函数名匹配,因为您的问题显示的是post,但通过post访问。