【发布时间】:2020-07-20 10:06:56
【问题描述】:
我有 3 个实体类别、帖子和页面。 Category 和 Post 之间的关系属于 many(并且工作正常),Page 和 Category 之间的关系也属于 many(它也工作正常)。现在我想检索页面中所有类别的帖子。有没有办法在页面和帖子之间建立直接关系? 我的表格结构:
categories: int id string title.
posts: int id text content.
categories_posts: int id int category_id int post_id.
pages: int id string title.
pages_categories: int id int page_id int category_id.
【问题讨论】:
-
这有点棘手,您可以尝试以下代码吗?
class Page extends Model { public function posts() { return $this->hasManyThrough( 'App\Post', 'App\PagesCategory', 'category_id', // Foreign key on pages_categories table... 'id', // Foreign key on posts table... 'id', // Local key on pages table... 'user_id' // Local key on pages_categories table... ); } } -
我已经尝试过了,但是没有用。
标签: laravel eloquent laravel-5.8 eloquent-relationship