【问题标题】:How from Forum model refer forumPosts?论坛模型如何引用论坛帖子?
【发布时间】:2020-11-14 07:02:59
【问题描述】:

在 laravel 7 应用程序中我 app/Forum.php 使用方法模型:

public function forumThreads()
{
    return $this->hasMany('App\ForumThread', 'forum_id', 'id');
}

app/ForumThread.php 具有字段 forum_id 和: 公共功能论坛() { return $this->belongsTo('App\Forum', 'id'); }

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

在 app/ForumPost.php 中有字段 forum_thread_id 和:

public function forumThread()
{
    return $this->belongsTo('App\ForumThread'/*, 'user_id'*/);
}

我可以在 app/Forum.php 中制作引用帖子的方法吗:

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

如果是,怎么做?

谢谢!

【问题讨论】:

  • 检查“有很多通过”关系

标签: laravel eloquent


【解决方案1】:

虽然你的口才看起来令人困惑,但我会说猜测可能需要使用“有很多通过”和“有一个通过”的关系。

【讨论】:

  • 奇怪,这里laravel.com/docs/7.x/eloquent没有描述这些方法。请提供描述它们的链接
  • @mstdmstd 在 Eloquent 的“关系”文档中,因为它是一种“关系”......顺便说一句,文档页面顶部有一个搜索栏
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多