Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fbc's avatar
Level 2

Query all records with a +/- variance of 20% of $xvariable?

How would you make a query of all records with a variance of 20% of values in a column from $xvariable?

0 likes
2 replies
Dalma's avatar

Maybe something like:

$query = model::where('field' , '>=' , ($xvariable * .8)) -> where('field', '<=', ($xvariable * 1.2))->get();
martinbean's avatar
Level 80

@fbc Using a whereBetween() clause:

$results = Model::whereBetween('column', [
    ($value * .8), // -20%
    ($value * 1.2), // +20%
])->get();

Please or to participate in this conversation.