我有一个 ManyToMany 关系
用户控制器
public function measurement()
{
return $this->belongsToMany('Measurement', 'users_measurement')->withPivot('value');
}
列结构
user_id | measurement_id | value
搜索查询
->whereHas('measurement', function($q) use ( $from, $to )
{
foreach (array_combine($from, $to) as $f => $t) {
if(!empty($f) && !empty($t))
{
$q->whereRaw('( users_measurement.value between ? and ?)', array($f, $t));
}
}
})
错误
(select count(*) from `lara_measurements` inner join `lara_users_measurement` on `lara_measurements`.`id` = `lara_users_measurement`.`measurement_id` where `lara_users_measurement`.`user_id` = `lara_users`.`id` and ( users_measurement.value between 154 cm and 168 cm) and ( users_measurement.value between 47 kg and 53 kg))
如果我使用
$q->whereRaw('( mesurement_id between ? and ?)', array($f, $t));
作品
如果使用
$q->whereRaw('( user_id between ? and ?)', array($f, $t));
再次工作
将这些仅用于测试,但为什么它不能与值列一起使用?