Skip to content

Laravel whereBetween, whereBetweenColumns vs whereValueBetween Explained with Examples

Learn how to use Laravel’s whereBetween, whereBetweenColumns, and whereValueBetween with examples for queries, filtering, and clean Eloquent code.

Dasun Kalupahana 2 min read 648 views 0 comments

When working with Laravel’s query builder, filtering records within a range is a common requirement. Whether you’re checking booking dates, product prices, or attendance times, Laravel provides three powerful helpers: whereBetween, whereBetweenColumns, and whereValueBetween.

Let’s walk through each method, see practical examples, and understand when to use which.


Quick overview

  • whereBetween :- A column’s value is between two fixed values.
  • whereBetweenColumns :- A column’s value is between two other columns in the same row.
  • whereValueBetween :- A fixed value is between two column values in the same row.

1. whereBetween

What it does:
Filters rows where a column is between two given values (inclusive).

Example: Fetch reservations made within a certain number of guests.

$reservations = DB::table('reservations')
    ->whereBetween('guest_count', [2, 6])
    ->get();

SQL:

SELECT * FROM `reservations`
WHERE `guest_count` BETWEEN 2 AND 6;

Use-cases:

  • Hotel rooms priced between $100 and $300.
  • Fetch sales between two specific dates.
  • Attendance where “hours_worked” falls between minimum and maximum thresholds.

2. whereBetweenColumns

What it does:
Compares a column with two other columns in the same row.

Example: Find packages where the current weight is valid between stored min and max limits.

$packages = DB::table('packages')
    ->whereBetweenColumns('current_weight', ['min_weight', 'max_weight'])
    ->get();

SQL:

SELECT * FROM `packages`
WHERE `current_weight` BETWEEN `min_weight` AND `max_weight`;

Use-cases:

  • Employees whose salary is between their department’s min and max salary columns.
  • Flights where actual departure time is between scheduled start and end windows.
  • Students whose marks fall within allowed grading thresholds.

3. whereValueBetween

What it does:
Instead of checking a column, you check a fixed value against two columns.

Example: Fetch events happening right now.

use Carbon\Carbon;

$now = Carbon::now();

$events = DB::table('events')
    ->whereValueBetween($now, ['start_time', 'end_time'])
    ->get();

SQL:

SELECT * FROM `events`
WHERE '2025-09-19 14:35:00' BETWEEN `start_time` AND `end_time`;

Use-cases:

  • Show promotions valid on today’s date.
  • Filter classes where the current time is between class_start and class_end.
  • Fetch subscriptions where the given user age fits between min_age and max_age.

Real-world examples

Example A: Products within a price range chosen by user

$products = DB::table('products')
    ->whereBetween('price', [500, 1500])
    ->get();

Example B: Rooms available within stored seasonal ranges

$rooms = DB::table('rooms')
    ->whereBetweenColumns('capacity', ['min_capacity', 'max_capacity'])
    ->get();

Example C: Active coupons

$today = now();

$coupons = DB::table('coupons')
    ->whereValueBetween($today, ['valid_from', 'valid_until'])
    ->get();

When not to use them

  • Complex logic requiring joins → may need whereRaw.
  • Calculations inside comparisons → might not be index-friendly.
  • Large datasets with unindexed columns → test performance.

Conclusion

  • Use whereBetween → column checked against fixed values.
  • Use whereBetweenColumns → column checked against other columns.
  • Use whereValueBetween → fixed value checked against dynamic column range.

If you found this helpful, feel free to share or drop a comment. Happy coding with Laravel! 🧱✨

Free resources for this topic

Code snippets and fixes for this topic

Get one practical tip every week

New tutorials, fixes and free cheat sheets from DEV Talk, straight to your inbox. No spam, unsubscribe in one click.

Free download: Laravel interview questions (PDF)

Have a question about this post?

Ask the community: other developers and the author can answer.

Ask the community

Get notified about new articles

One click, no email needed. Turn it off anytime with the bell at the top.

Get new articles on your iPhone

Add DEV Talk to your Home Screen, open it, then tap the bell.

Rate this post
0.0
Share

Enjoying the content?

If this article helped you, you can support the author with a coffee. It keeps new posts coming.

Buy me a coffee (opens in a new tab)

Comments 0

Be kind. Comments with links are reviewed before they appear.

No comments yet

Be the first to share your thoughts.

Get new articles in your inbox

Practical tutorials and deep dives, about once a week. No spam, unsubscribe anytime.

Privacy policy

Add DEV Talk to your Home Screen

  1. 1 Tap the Share button in Safari's toolbar.
  2. 2 Scroll down and tap Add to Home Screen.
  3. 3 Tap Add. Open it from the Home Screen to read like an app and to turn on notifications with the bell.

We use cookies to improve your experience and analyze traffic. By continuing, you agree to our Privacy Policy.