关于 “关联模型的问题”?


问题是这样:
表1是comment(评论)表,字段是id,content,post_id
表2是post(文章)表,字段是id,title

一条评论 属于 一个篇文章
comment.post_id和post.id关联

class Post extends Model{
use SoftDeletes;

protected $table = 'posts';
protected $primaryKey = 'id';
protected $fillable = ['title','content','user_id'];
}
class Comment extends Model{
use SoftDeletes;
protected $table = 'comments';
protected $primaryKey = 'id';
protected $fillable = ['content','user_id','post_id'];
public function post() {
return $this->belongsTo(Post::class,'post_id','id');
}
}

问题,,我在blade.php中写

{{$comment->post}}
,能打印出整个post的信息,但是{{$comment->post->title}},,就报错:“Trying to get property of non-object ”,我就纳闷了,$comment->post都能打印出全部信息,但是我单个去title就报object为null,,请各位大哥大姐指点一下,我哪里写错了,小弟先谢谢了


点赞 取消点赞 收藏 取消收藏

<< 上一篇: 新建项目出现错误:Parse error: syntax error, unexpected '?'

>> 下一篇: Laravel5.4 升级5.5之后,错误页面不显示详细信息了