laravel - 获取与所选评论关联的帖子内容(方法帖子不存在)
问题描述
我有这段代码来获取与所选评论关联的帖子内容:
$postContent = Comment::find($commentID)->post()->first();
但它显示“方法帖子不存在。”。
评论型号:
public function post(){
return $this->belongsTo('App\Post', 'post_id');
}
岗位型号:
public function comments(){
return $this->hasMany('App\Comment', 'post_id');
}
解决方案
试试下面的代码:
$postContent = Comment::find($commentID)->post;
推荐阅读
- mysql - 如何在 jupyter notebook 中将默认数据库从 sqlite 更改为 postgresql/mysql
- mysql - 当我在 mySQL 上的创建过程中创建变量时出现错误
- parsing - 秒差距:停在空行
- ruby-on-rails - 如何正确扩展 ActiveSupport::Concern?
- anylogic - 在 Anylogic PLE 中训练 RL4J 模型时,什么可能导致状态图不更新?
- keras - Keras 中具有赔率的 2 人游戏的自定义损失函数
- html - 谷歌图片搜索图片网址
- javascript - Webpack & Babel - nodejs 和 yarn 更新后无法运行 dev-server
- visual-studio-code - 如何在任务栏中设置 Visual Studio Code 以显示最近的工作区?
- python - 在python中搜索矩阵中的值