I have a ManyToMany relation
user_id | measurement_id | value
Search query
->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));
}
}
})
and i get the following error
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'value' in where clause is ambiguous (SQL: select count(*) as aggregate
Tired with the following query
$q->whereRaw('( users_measurement.value between ? and ?)', array($f, $t));
Than i get this error
Column not found: 1054 Unknown column 'users_measurement.value' in 'where clause'
Could please someone help me out?
Edit more specific queries
Users model
public function measurement()
{
return $this->belongsToMany('Measurement', 'users_measurement')->withPivot('value');
}
Search Query in my controller
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);
}