Support the ongoing development of Laravel.io →
Database Eloquent Architecture

I have a ManyToMany relation

Users controller

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

Column structure

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));

                    }
                }
        })

Error

(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))

if i use

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

works

if i use

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

works again

Made these just for a test, but why does it not working with value column ?

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Ronto ronto Joined 27 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.