I have a laravel project and in this project there is a table like this:
I need to run a query and return a result where the $circulation
variable is in range of the column range_from
and range_to
.
So I tried this:
$tariff = StoreCategoryTariff::where('option_value', $side)
->whereBetween('range_from', [$circulation, 'range_to'])
->orWhereBetween('range_to', [$circulation, 'range_from'])
->first()->price;
But this is wrong..
I also tried this one:
$tariff = StoreCategoryTariff::where('option_value', $side)
->where('range_from', '>=', $circulation)
->where('range_from', '<=', $circulation)
->first()->price;
But didn’t get the result. So how to properly check for the result where circulation
is in range ?
4
Answers
There’s no built-in method for this exact use case.
I would recommend using a raw where:
This should get all rows where
$circulation
is contained between therange_from
andrange_to
I think all you had to do was use the range_to instead of range_from again:
Try using nested where closure
You should use
>=
forrange_from
and<=
for range_to.You can try it. I think it will be solve your problem: