Skip to content

Conversation

@jfBiswajit
Copy link

Description

This PR improves the whereBetween method in Laravel's Query Builder so it can work smoothly with PHP's DatePeriod class. It also fixes a problem where periods created using a recurrence count (rather than an end date) would previously cause errors.

What Changed

whereBetween now supports DatePeriod in addition to CarbonPeriod. Periods created with either an end date or a recurrence count will work as expected. All existing uses of CarbonPeriod remain fully compatible, and tests have been added to ensure this behavior.

Why This Matters

Before this change, whereBetween had two main issues:

  1. It only worked with CarbonPeriod, not the base DatePeriod.
  2. It failed when DatePeriod objects were created using a recurrence count instead of an explicit end date.

This PR resolves both issues, making period-based queries more flexible and reliable. Support for additional period options like EXCLUDE_START_DATE could be added in a future PR.

Examples

Before

$period = new DatePeriod(now(), new DateInterval('P1M'), 2);
Order::whereBetween('created_at', $period)->get(); // Error

After

// Using an end date
$period1 = new DatePeriod(now(), new DateInterval('P2M'), now()->addMonths(2));
Order::whereBetween('created_at', $period1)->get(); // Works

// Using recurrence count
$period2 = new DatePeriod(now(), new DateInterval('P2M'), 2);
Order::whereBetween('created_at', $period2)->get(); // Works

…ween

- Add support for PHP's DatePeriod class in addition to CarbonPeriod
- Fix issue where DatePeriod created with recurrences fails
- Calculate end date by iterating period when no explicit end date
- Add comprehensive tests for DatePeriod with end dates and recurrences
- Maintain backward compatibility with existing CarbonPeriod usage

Fixes laravel#58092
@jfBiswajit jfBiswajit changed the title [12.x] Add DatePeriod support and fix recurrence handling in whereBet… [12.x] Add DatePeriod support and fix recurrence handling in whereBetween Dec 12, 2025

if ($values instanceof CarbonPeriod) {
$values = [$values->getStartDate(), $values->getEndDate()];
if ($values instanceof \DatePeriod || $values instanceof CarbonPeriod) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CarbonPeriod extends DataPeriod, so the OR is not necessary.

@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

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.

3 participants