Laravel 多态多对多通过特定关联获取
Laravel Polymorphic Many To Many Fetch By Specific Association
我有一个简单的多对多多态关系(如果可能的话):
作者:
在博客上,我们有 Posts => 可授权
在杂志上,文章 => 可创作
两种关系都作为 expected/documented。我需要的是获取特定 Post 类别的所有作者
我只有:Post::category('blue')
集合(类别是一个范围)。基于此,获得写 "Blue Posts" 的作者的最佳方法是什么?
您需要研究在您的范围内使用 has()
和 whereHas()
查询方法。有关使用它们的详细说明,请参阅 。
基本上看起来像这样:
$authorsOfBluePosts = Author::whereHas('posts', function ($postsQuery) {
$postsQuery->whereHas('category', function ($categoryQuery) {
$categoryQuery->where('name', 'blue');
});
})->get();
我有一个简单的多对多多态关系(如果可能的话):
作者: 在博客上,我们有 Posts => 可授权 在杂志上,文章 => 可创作
两种关系都作为 expected/documented。我需要的是获取特定 Post 类别的所有作者
我只有:Post::category('blue')
集合(类别是一个范围)。基于此,获得写 "Blue Posts" 的作者的最佳方法是什么?
您需要研究在您的范围内使用 has()
和 whereHas()
查询方法。有关使用它们的详细说明,请参阅
基本上看起来像这样:
$authorsOfBluePosts = Author::whereHas('posts', function ($postsQuery) {
$postsQuery->whereHas('category', function ($categoryQuery) {
$categoryQuery->where('name', 'blue');
});
})->get();