Skip to content

Commit

Permalink
fix query builder whereBetween method with carbon period (#46720)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tegos committed Apr 10, 2023
1 parent 1e129e6 commit a24b75e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 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 @@ -1253,7 +1253,7 @@ public function whereBetween($column, iterable $values, $boolean = 'and', $not =
$type = 'between';

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

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

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

$this->havings[] = compact('type', 'column', 'values', 'boolean', 'not');
Expand Down
9 changes: 8 additions & 1 deletion tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,14 @@ public function testWhereBetweens()
$period = now()->toPeriod(now()->addDay());
$builder->select('*')->from('users')->whereBetween('created_at', $period);
$this->assertSame('select * from "users" where "created_at" between ? and ?', $builder->toSql());
$this->assertEquals($period->toArray(), $builder->getBindings());
$this->assertEquals([$period->start, $period->end], $builder->getBindings());

// custom long carbon period date
$builder = $this->getBuilder();
$period = now()->toPeriod(now()->addMonth());
$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());

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

0 comments on commit a24b75e

Please sign in to comment.