Skip to content

Commit a24b75e

Browse files
authored
fix query builder whereBetween method with carbon period (#46720)
1 parent 1e129e6 commit a24b75e

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/Illuminate/Database/Query/Builder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,7 +1253,7 @@ public function whereBetween($column, iterable $values, $boolean = 'and', $not =
12531253
$type = 'between';
12541254

12551255
if ($values instanceof CarbonPeriod) {
1256-
$values = $values->toArray();
1256+
$values = [$values->start, $values->end];
12571257
}
12581258

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

22342234
if ($values instanceof CarbonPeriod) {
2235-
$values = $values->toArray();
2235+
$values = [$values->start, $values->end];
22362236
}
22372237

22382238
$this->havings[] = compact('type', 'column', 'values', 'boolean', 'not');

tests/Database/DatabaseQueryBuilderTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,14 @@ public function testWhereBetweens()
770770
$period = now()->toPeriod(now()->addDay());
771771
$builder->select('*')->from('users')->whereBetween('created_at', $period);
772772
$this->assertSame('select * from "users" where "created_at" between ? and ?', $builder->toSql());
773-
$this->assertEquals($period->toArray(), $builder->getBindings());
773+
$this->assertEquals([$period->start, $period->end], $builder->getBindings());
774+
775+
// custom long carbon period date
776+
$builder = $this->getBuilder();
777+
$period = now()->toPeriod(now()->addMonth());
778+
$builder->select('*')->from('users')->whereBetween('created_at', $period);
779+
$this->assertSame('select * from "users" where "created_at" between ? and ?', $builder->toSql());
780+
$this->assertEquals([$period->start, $period->end], $builder->getBindings());
774781

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

0 commit comments

Comments
 (0)