-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
[10.x] Fix query builder whereBetween method with carbon date period #46720
Conversation
What's happening here? I dont understand the code comment above. |
|
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' |
…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.
…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.
…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.
Fixes issue when using query builder method whereBetween with param of custom carbon date period.