Skip to content

Commit

Permalink
[11.x] Fix query builder whereBetween with CarbonPeriod and Carbon 3 (
Browse files Browse the repository at this point in the history
#50792)

* Use `getStartDate()` and `getEndDate()`

* Fix test
  • Loading branch information
bakerkretzmar committed Mar 27, 2024
1 parent e740b58 commit 488e251
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1316,7 +1316,7 @@ public function whereBetween($column, iterable $values, $boolean = 'and', $not =
$type = 'between';

if ($values instanceof CarbonPeriod) {
$values = [$values->start, $values->end];
$values = [$values->getStartDate(), $values->getEndDate()];
}

$this->wheres[] = compact('type', 'column', 'values', 'boolean', 'not');
Expand Down Expand Up @@ -2381,7 +2381,7 @@ public function havingBetween($column, iterable $values, $boolean = 'and', $not
$type = 'between';

if ($values instanceof CarbonPeriod) {
$values = [$values->start, $values->end];
$values = [$values->getStartDate(), $values->getEndDate()];
}

$this->havings[] = compact('type', 'column', 'values', 'boolean', 'not');
Expand Down
8 changes: 4 additions & 4 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -771,17 +771,17 @@ public function testWhereBetweens()
$this->assertEquals([], $builder->getBindings());

$builder = $this->getBuilder();
$period = now()->toPeriod(now()->addDay());
$period = now()->startOfDay()->toPeriod(now()->addDay()->startOfDay());
$builder->select('*')->from('users')->whereBetween('created_at', $period);
$this->assertSame('select * from "users" where "created_at" between ? and ?', $builder->toSql());
$this->assertEquals([$period->start, $period->end], $builder->getBindings());
$this->assertEquals([now()->startOfDay(), now()->addDay()->startOfDay()], $builder->getBindings());

// custom long carbon period date
$builder = $this->getBuilder();
$period = now()->toPeriod(now()->addMonth());
$period = now()->startOfDay()->toPeriod(now()->addMonth()->startOfDay());
$builder->select('*')->from('users')->whereBetween('created_at', $period);
$this->assertSame('select * from "users" where "created_at" between ? and ?', $builder->toSql());
$this->assertEquals([$period->start, $period->end], $builder->getBindings());
$this->assertEquals([now()->startOfDay(), now()->addMonth()->startOfDay()], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereBetween('id', collect([1, 2]));
Expand Down

0 comments on commit 488e251

Please sign in to comment.