Skip to content

Commit 6bfd755

Browse files
lozobojanBojan Lozotaylorotwell
authored
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 <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent 616f81b commit 6bfd755

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/Illuminate/Database/Query/Grammars/Grammar.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Illuminate\Database\Query\Grammars;
44

5+
use Illuminate\Contracts\Database\Query\Expression;
56
use Illuminate\Database\Concerns\CompilesJsonPaths;
67
use Illuminate\Database\Grammar as BaseGrammar;
78
use Illuminate\Database\Query\Builder;
@@ -246,7 +247,7 @@ protected function concatenateWhereClauses($query, $sql)
246247
*/
247248
protected function whereRaw(Builder $query, $where)
248249
{
249-
return $where['sql'];
250+
return $where['sql'] instanceof Expression ? $where['sql']->getValue($this) : $where['sql'];
250251
}
251252

252253
/**
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Database;
4+
5+
use Illuminate\Database\Query\Builder;
6+
use Illuminate\Database\Query\Expression;
7+
use Illuminate\Database\Query\Grammars\Grammar;
8+
use Mockery as m;
9+
use PHPUnit\Framework\TestCase;
10+
use ReflectionClass;
11+
12+
class DatabaseQueryGrammarTest extends TestCase
13+
{
14+
protected function tearDown(): void
15+
{
16+
m::close();
17+
}
18+
19+
public function testWhereRawReturnsStringWhenExpressionPassed()
20+
{
21+
$builder = m::mock(Builder::class);
22+
$grammar = new Grammar;
23+
$reflection = new ReflectionClass($grammar);
24+
$method = $reflection->getMethod('whereRaw');
25+
$expressionArray = ['sql' => new Expression('select * from "users"')];
26+
27+
$rawQuery = $method->invoke($grammar, $builder, $expressionArray);
28+
29+
$this->assertSame('select * from "users"', $rawQuery);
30+
}
31+
32+
public function testWhereRawReturnsStringWhenStringPassed()
33+
{
34+
$builder = m::mock(Builder::class);
35+
$grammar = new Grammar;
36+
$reflection = new ReflectionClass($grammar);
37+
$method = $reflection->getMethod('whereRaw');
38+
$stringArray = ['sql' => 'select * from "users"'];
39+
40+
$rawQuery = $method->invoke($grammar, $builder, $stringArray);
41+
42+
$this->assertSame('select * from "users"', $rawQuery);
43+
}
44+
}

0 commit comments

Comments
 (0)