Skip to content
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

[10.x] Fix query builder whereBetween method with carbon date period #46720

Merged

Conversation

Tegos
Copy link
Contributor

@Tegos Tegos commented Apr 8, 2023

Fixes issue when using query builder method whereBetween with param of custom carbon date period.

$from = '01.03.2023';
$to = '31.03.2023';

$period = CarbonPeriod::create($from, $to);

$orders = Order::query()->whereBetween('created_at', $period)->get();

// the query will be
// select * from `orders` where `created_at` between '2023-03-01 00:00:00' and '2023-03-02 00:00:00'
// instead select * from `orders` where `created_at` between '2023-03-01 00:00:00' and '2023-03-31 00:00:00'

@bert-w
Copy link
Contributor

bert-w commented Apr 8, 2023

What's happening here? I dont understand the code comment above.

@Rizky92
Copy link

Rizky92 commented Apr 9, 2023

What's happening here? I dont understand the code comment above.

CarbonPeriod generates list of dates on daily interval when converted into array. So instead of grabbing first and last date, it grabs the first and next date on the array.

@Tegos
Copy link
Contributor Author

Tegos commented Apr 9, 2023

Hello, @bert-w. Don't worry, mate.

In the query builder, method whereBetween accept second parameter iterable $values, which can be a CarbonPeriod instance.

But when use CarbonPeriod parameter the method don't work properly, it take only first date and next date insted last date of period.

See example

$from = Carbon::now()->startOfYear(); // 2023-01-01
$to = Carbon::now()->endOfYear(); // 2023-12-31

$period = CarbonPeriod::create($from, $to);

$orders = Order::query()->whereBetween('created_at', $period)->get();
#result query
select * from `orders` where `created_at` between '2023-01-01 00:00:00' and '2023-01-02 00:00:00'

'2023-01-01 00:00:00' and '2023-01-02 00:00:00'

@taylorotwell taylorotwell merged commit a24b75e into laravel:10.x Apr 10, 2023
17 checks passed
GromNaN added a commit to GromNaN/laravel-mongodb that referenced this pull request Jul 19, 2023
…eject invalid array (#10)

The Query\Builder::whereBetween() method can be used like this:

whereBetween('date_field', [min, max])
whereBetween('date_field', collect([min, max]))
whereBetween('date_field', CarbonPeriod)

Laravel allows other formats: the $values array is flatten and the builder assumes there are at least 2 elements and ignore the others. It's a design that can lead to misunderstandings. I prefer to raise an exception when we have incorrect values, rather than trying to guess what the developer would like to do.

Support for CarbonPeriod was fixed in Laravel 10: laravel/framework#46720 because the query builder was taking the 1st 2 values of the iterator instead of the start & end dates.
alcaeus pushed a commit to mongodb/laravel-mongodb that referenced this pull request Aug 22, 2023
…eject invalid array (#10)

The Query\Builder::whereBetween() method can be used like this:

whereBetween('date_field', [min, max])
whereBetween('date_field', collect([min, max]))
whereBetween('date_field', CarbonPeriod)

Laravel allows other formats: the $values array is flatten and the builder assumes there are at least 2 elements and ignore the others. It's a design that can lead to misunderstandings. I prefer to raise an exception when we have incorrect values, rather than trying to guess what the developer would like to do.

Support for CarbonPeriod was fixed in Laravel 10: laravel/framework#46720 because the query builder was taking the 1st 2 values of the iterator instead of the start & end dates.
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.

None yet

4 participants