Laravel - SQLSTATE [23000]:完整性约束违规:1052列'值'在where子句中含糊不清

时间:2014-09-03 09:13:10

标签: php mysql sql laravel-4

我有一个ManyToMany关系

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('( value between ? and  ?)', array($f, $t));

                    }
                }
        })

我收到以下错误

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'value' in where clause is ambiguous (SQL: select count(*) as aggregate

厌倦了以下查询

$q->whereRaw('( users_measurement.value between ? and  ?)', array($f, $t));

比我收到此错误

Column not found: 1054 Unknown column 'users_measurement.value' in 'where clause'

可以请别人帮帮我吗?

修改更具体的查询

用户模型

public function measurement()
    {
        return $this->belongsToMany('Measurement', 'users_measurement')->withPivot('value');
    }

在我的控制器中搜索查询

function __construct(User $user)
    {
        $this->user = $user;


    }



public function search()
    {
    $users = $this->user
             ->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));
                        }
                    }
            })
           ->where('approved', '=', 1)
           ->where('activated', '=', 1)
           ->paginate(15);
}

0 个答案:

没有答案