XXX's avatar
Level 5

Eloquent range (from/to)

For example, my database structure is:

+------------+----------+
| Range from | Range To |
+------------+----------+
|        100 |      200 |
|        220 |      250 |
|        500 |      600 |
|        620 |      700 |
+------------+----------+

I need to check the existence of number 500. That should return true because there is a row 500-600. Also 660 should return true because there is a row 620-700.

How can I query this with Eloquent efficiently? Sort of between with dates.

Thnx in advance.

0 likes
1 reply
lostdreamer_nl's avatar

Just about the same as any other SQL Query:

$numToFind = 500;

if(Model::where('range_from', '>=', $numToFind)->where('range_to', '<=', $numToFind)->count()) {
    // It exists...
}

Please or to participate in this conversation.