From 10843be745edff0b228b7bc78ec1ec1e8dd9cbb5 Mon Sep 17 00:00:00 2001 From: Bojan Lozo <69368941+lozobojan@users.noreply.github.com> Date: Mon, 9 Oct 2023 20:36:56 +0200 Subject: [PATCH] Fixed issue: Added a call to the getValue method (#48652) * Fixed issue: Added a call to the getValue method. Without this, raw SQL queries in conditions throw an exception. * Added check to prevent call on types other than Expression * Added new test cases for the modified method * Refactored the variable names * Fixed a StyleCI issue * Update Grammar.php --------- Co-authored-by: Bojan Lozo Co-authored-by: Taylor Otwell --- .../Database/Query/Grammars/Grammar.php | 3 +- tests/Database/DatabaseQueryGrammarTest.php | 44 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 tests/Database/DatabaseQueryGrammarTest.php diff --git a/src/Illuminate/Database/Query/Grammars/Grammar.php b/src/Illuminate/Database/Query/Grammars/Grammar.php index 5419ad07fbb3..3b4f117693f6 100755 --- a/src/Illuminate/Database/Query/Grammars/Grammar.php +++ b/src/Illuminate/Database/Query/Grammars/Grammar.php @@ -2,6 +2,7 @@ namespace Illuminate\Database\Query\Grammars; +use Illuminate\Contracts\Database\Query\Expression; use Illuminate\Database\Concerns\CompilesJsonPaths; use Illuminate\Database\Grammar as BaseGrammar; use Illuminate\Database\Query\Builder; @@ -246,7 +247,7 @@ protected function concatenateWhereClauses($query, $sql) */ protected function whereRaw(Builder $query, $where) { - return $where['sql']; + return $where['sql'] instanceof Expression ? $where['sql']->getValue($this) : $where['sql']; } /** diff --git a/tests/Database/DatabaseQueryGrammarTest.php b/tests/Database/DatabaseQueryGrammarTest.php new file mode 100644 index 000000000000..aee7f822caba --- /dev/null +++ b/tests/Database/DatabaseQueryGrammarTest.php @@ -0,0 +1,44 @@ +getMethod('whereRaw'); + $expressionArray = ['sql' => new Expression('select * from "users"')]; + + $rawQuery = $method->invoke($grammar, $builder, $expressionArray); + + $this->assertSame('select * from "users"', $rawQuery); + } + + public function testWhereRawReturnsStringWhenStringPassed() + { + $builder = m::mock(Builder::class); + $grammar = new Grammar; + $reflection = new ReflectionClass($grammar); + $method = $reflection->getMethod('whereRaw'); + $stringArray = ['sql' => 'select * from "users"']; + + $rawQuery = $method->invoke($grammar, $builder, $stringArray); + + $this->assertSame('select * from "users"', $rawQuery); + } +}