![](/img/trans.png)
[英]LARAVEL - how to retrieve data from three tables connected through two pivot tables
[英]Laravel - Retrieve collections through two pivot tables
我目前在Laravel 5.6中遇到以下问题,我们有以下数据库表:
我正在寻找一种基于网站ID检索帖子的好方法。 一个网站可以分配多个提供者(通过website_providers),提供者可以分配多个帖子(他们也可以提供一个自定义的post_name,它应该取代标准的posts.name)。
在Laravel中实现此目标的最佳方法是什么? 我已经尝试通过关系来做到这一点,但不能使其顺利进行。 我还尝试使用一个很长的联接查询来加载帖子,但是感觉不对,必须有一种更简单,更漂亮的方法。
提前致谢!
在这种情况下没有本机关系。
我创建了一个HasManyThrough
与支持的关系为BelongsToMany
: 库GitHub上
安装后,您可以像这样使用它:
class Website extends Model {
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
public function posts() {
return $this->hasManyDeep(
Post::class,
['website_providers', Provider::class, 'provider_posts']
)->withPivot('provider_posts', ['post_name']);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.