假设我想存储有关客户的各种数据,因此我有两个由数据透视表链接的模型,在数据透视表中存储每个数据字段类型的客户值:
Customer {
public function datafields()
{
return $this->belongsToMany('Datafield')->withPivot('value');
}
}
和
Datafield {
public function customers()
{
return $this->belongsToMany('Customer')->withPivot('value');
}
所以我的表是客户,customer_datafield,datafields .
如何在客户中设置查询范围,以查找具有特定数据字段x值的所有客户?
有点像
Customer {
public function datafields()
{
return $this->belongsToMany('Datafield')->withPivot('value');
}
public function scopeSearch($query, $searchfor)
{
return $query->datafields()->pivot()
->where('value', $searchfor)
->where('datafield_id', 123);
}
}
我尝试了一些方法,但没有任何运气让人工作 . 任何建议非常感谢!
2 回答
单个固定枢轴字段的雄辩方式:
这为您提供更多功能和灵活性:
您可能想在第二个闭包中使用带有数组值的
where
而不是foreach
,但是它可能无法按预期工作,因为字段不会以表名为前缀 .另一种解决方案是使用简单的
join
:你可以像上面第二个例子一样改变它 .
注意:在Laravel 5上你可以简单地使用它: