diff --git a/src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php b/src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php index 4c9e26172e2d..65604fecda19 100755 --- a/src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php +++ b/src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php @@ -140,7 +140,7 @@ protected function dateBasedWhere($type, Builder $query, $where) { $value = $this->parameter($where['value']); - return "strftime('{$type}', {$this->wrap($where['column'])}) {$where['operator']} {$value}"; + return "strftime('{$type}', {$this->wrap($where['column'])}) {$where['operator']} cast({$value} as text)"; } /** diff --git a/tests/Database/DatabaseQueryBuilderTest.php b/tests/Database/DatabaseQueryBuilderTest.php index 963b81baace7..467cb8a12b51 100755 --- a/tests/Database/DatabaseQueryBuilderTest.php +++ b/tests/Database/DatabaseQueryBuilderTest.php @@ -451,19 +451,19 @@ public function testWhereDateSqlite() { $builder = $this->getSQLiteBuilder(); $builder->select('*')->from('users')->whereDate('created_at', '=', '2015-12-21'); - $this->assertEquals('select * from "users" where strftime(\'%Y-%m-%d\', "created_at") = ?', $builder->toSql()); + $this->assertEquals('select * from "users" where strftime(\'%Y-%m-%d\', "created_at") = cast(? as text)', $builder->toSql()); $this->assertEquals([0 => '2015-12-21'], $builder->getBindings()); $builder = $this->getSQLiteBuilder(); $builder->select('*')->from('users')->whereDate('created_at', new Raw('NOW()')); - $this->assertEquals('select * from "users" where strftime(\'%Y-%m-%d\', "created_at") = NOW()', $builder->toSql()); + $this->assertEquals('select * from "users" where strftime(\'%Y-%m-%d\', "created_at") = cast(NOW() as text)', $builder->toSql()); } public function testWhereDaySqlite() { $builder = $this->getSQLiteBuilder(); $builder->select('*')->from('users')->whereDay('created_at', '=', 1); - $this->assertEquals('select * from "users" where strftime(\'%d\', "created_at") = ?', $builder->toSql()); + $this->assertEquals('select * from "users" where strftime(\'%d\', "created_at") = cast(? as text)', $builder->toSql()); $this->assertEquals([0 => 1], $builder->getBindings()); } @@ -471,7 +471,7 @@ public function testWhereMonthSqlite() { $builder = $this->getSQLiteBuilder(); $builder->select('*')->from('users')->whereMonth('created_at', '=', 5); - $this->assertEquals('select * from "users" where strftime(\'%m\', "created_at") = ?', $builder->toSql()); + $this->assertEquals('select * from "users" where strftime(\'%m\', "created_at") = cast(? as text)', $builder->toSql()); $this->assertEquals([0 => 5], $builder->getBindings()); } @@ -479,7 +479,7 @@ public function testWhereYearSqlite() { $builder = $this->getSQLiteBuilder(); $builder->select('*')->from('users')->whereYear('created_at', '=', 2014); - $this->assertEquals('select * from "users" where strftime(\'%Y\', "created_at") = ?', $builder->toSql()); + $this->assertEquals('select * from "users" where strftime(\'%Y\', "created_at") = cast(? as text)', $builder->toSql()); $this->assertEquals([0 => 2014], $builder->getBindings()); } @@ -487,7 +487,7 @@ public function testWhereTimeSqlite() { $builder = $this->getSQLiteBuilder(); $builder->select('*')->from('users')->whereTime('created_at', '>=', '22:00'); - $this->assertEquals('select * from "users" where strftime(\'%H:%M:%S\', "created_at") >= ?', $builder->toSql()); + $this->assertEquals('select * from "users" where strftime(\'%H:%M:%S\', "created_at") >= cast(? as text)', $builder->toSql()); $this->assertEquals([0 => '22:00'], $builder->getBindings()); } @@ -495,7 +495,7 @@ public function testWhereTimeOperatorOptionalSqlite() { $builder = $this->getSQLiteBuilder(); $builder->select('*')->from('users')->whereTime('created_at', '22:00'); - $this->assertEquals('select * from "users" where strftime(\'%H:%M:%S\', "created_at") = ?', $builder->toSql()); + $this->assertEquals('select * from "users" where strftime(\'%H:%M:%S\', "created_at") = cast(? as text)', $builder->toSql()); $this->assertEquals([0 => '22:00'], $builder->getBindings()); } diff --git a/tests/Integration/Database/QueryBuilderTest.php b/tests/Integration/Database/QueryBuilderTest.php new file mode 100644 index 000000000000..3c917d7f78c2 --- /dev/null +++ b/tests/Integration/Database/QueryBuilderTest.php @@ -0,0 +1,54 @@ +timestamp('created_at'); + }); + + DB::table('posts')->insert([ + ['created_at' => new Carbon('2017-11-12 13:14:15')], + ['created_at' => new Carbon('2018-01-02 03:04:05')], + ]); + } + + public function testWhereDate() + { + $this->assertSame(1, DB::table('posts')->whereDate('created_at', '2018-01-02')->count()); + } + + public function testWhereDay() + { + $this->assertSame(1, DB::table('posts')->whereDay('created_at', '02')->count()); + } + + public function testWhereMonth() + { + $this->assertSame(1, DB::table('posts')->whereMonth('created_at', '01')->count()); + } + + public function testWhereYear() + { + $this->assertSame(1, DB::table('posts')->whereYear('created_at', '2018')->count()); + $this->assertSame(1, DB::table('posts')->whereYear('created_at', 2018)->count()); + } + + public function testWhereTime() + { + $this->assertSame(1, DB::table('posts')->whereTime('created_at', '03:04:05')->count()); + } +}