所以我有一个养老金类型:
class Type extends Base
{
use RecordStatusActiveTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'pn_pension_type';
/**
* The actual primary key for the model.
*
* @var string
*/
protected $primaryKey = 'pension_type_id';
// Belongs To -----
public function pensionClass() {
return $this->belongsTo('App'Models'Control'PensionClass', 'pension_class_id');
}
// Belongs To Many -----
public function qualifiers()
{
return $this->belongsToMany('App'Models'Pension'Qualifier', 'pn_pension_type_qualifier', 'type_id', 'qualifier_id');
}
}
和养老金预选赛:
class Qualifier extends Base
{
use RecordStatusActiveTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'pn_pension_qualifier';
/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts = [
'rules' => 'array',
];
// Belongs To Many -----
public function types()
{
return $this->belongsToMany('App'Models'Pension'Type', 'pn_pension_type_qualifier', 'qualifier_id', 'type_id');
}
}
他们与介于两者之间的桌子有多对多的关系,这是正常的。我想抓住一组养老金类型,并在所有这些类型之间找到独特的限定符。我可以通过他们的每个预选赛,并最终得到一个独特的预选赛列表,但我想知道他们是否是一种"雄辩"的方式。
使用 whereHas
将允许您根据关系限制结果。关于你想要什么,你想像这样从你的限定符中查询。
Qualifiers::whereHas('pensions', function($query) use ($pensionIds) {
$query->whereIn('id', $pensionIds);
})->distinct()->get();
添加->distinct()
将使您获得唯一的条目。