Skip to content

[12.x] Adds checking if a value is between two columns #56119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 12.x
Choose a base branch
from

Conversation

DarkGhostHunter
Copy link
Contributor

@DarkGhostHunter DarkGhostHunter commented Jun 24, 2025

What?

This PR allows the developer to check if a given value is between two columns. This complements both whereBetween() whereColumnsBetween() methods, as these can't check if a value (like an integer or timestamp) is between two columns. It's left to the developer to properly cast the values or column values as with the prior methods.

This avoids using raw statements with the danger of SQL Injections, but Raw statements are accepted as values and columns too. Yes, it also gets rid of using double WHERE clauses for the same purpose.

use App\Models\Post;

// Before
Post::whereRaw('? between "visible_from" and "visible_to"', now())->get();
Post::where('visible_from', '<=', now())->where('visible_to', '>=', now())->get();

// After
Post::whereValueBetween(now(), ['visible_from', 'visible_to'])->get();

The methods added are:

  • whereValueBetween()
  • orWhereValueBetween()
  • whereValueNotBetween()
  • orWhereValueNotBetween()

The only consideration is that the BETWEEN clause is inclusive (given SQL Standards), which is also used on the other between methods.

@DarkGhostHunter DarkGhostHunter force-pushed the feat/12.x/where-value-between-columns branch from c33edaf to beb6d1c Compare June 24, 2025 06:21
@shaedrich
Copy link
Contributor

Would it make sense to have a flag to allow for exclusive and half-open BETWEENs?

@DarkGhostHunter
Copy link
Contributor Author

Would it make sense to have a flag to allow for exclusive and half-open BETWEENs?

What do you mean? AFAIK, BETWEEN is inclusive and doesn't have flags for exclusivity, let alone more than two values/columns.

Anyway, let's see if this basic implementation is accepted first and then we can talk about adding more.

@DarkGhostHunter DarkGhostHunter changed the title [12.x] Adds checking a value between two columns [12.x] Adds checking if a value is between two columns Jun 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants