From ae4b96acbc45993c2671c30877574c5d223f25fd Mon Sep 17 00:00:00 2001 From: Lito Date: Tue, 18 Oct 2022 12:02:41 +0200 Subject: [PATCH 1/2] Added rawValue to Database Query Builder (and Eloquent as wrapper) --- src/Illuminate/Database/Eloquent/Builder.php | 1 + src/Illuminate/Database/Query/Builder.php | 14 ++++++++++++++ tests/Database/DatabaseQueryBuilderTest.php | 9 +++++++++ 3 files changed, 24 insertions(+) diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index 2f0c2fed57db..3beb4c8c89ff 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -110,6 +110,7 @@ class Builder implements BuilderContract 'max', 'min', 'raw', + 'rawValue', 'sum', 'toSql', ]; diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index 59f45d27acd9..99e8ed15511a 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -2596,6 +2596,20 @@ public function value($column) return count($result) > 0 ? reset($result) : null; } + /** + * Get a single expression value from the first result of a query. + * + * @param string $expression + * @param array $bindings + * @return mixed + */ + public function rawValue(string $expression, array $bindings = []) + { + if ($result = $this->selectRaw($expression, $bindings)->take(1)->first()) { + return reset($result); + } + } + /** * Get a single column's value from the first result of a query if it's the sole matching record. * diff --git a/tests/Database/DatabaseQueryBuilderTest.php b/tests/Database/DatabaseQueryBuilderTest.php index 6a0b6d97c231..d876ab332c3b 100755 --- a/tests/Database/DatabaseQueryBuilderTest.php +++ b/tests/Database/DatabaseQueryBuilderTest.php @@ -2464,6 +2464,15 @@ public function testValueMethodReturnsSingleColumn() $this->assertSame('bar', $results); } + public function testRawValueMethodReturnsSingleColumn() + { + $builder = $this->getBuilder(); + $builder->getConnection()->shouldReceive('select')->once()->with('select UPPER("foo") from "users" where "id" = ? limit 1', [1], true)->andReturn([['UPPER("foo")' => 'BAR']]); + $builder->getProcessor()->shouldReceive('processSelect')->once()->with($builder, [['UPPER("foo")' => 'BAR']])->andReturn([['UPPER("foo")' => 'BAR']]); + $results = $builder->from('users')->where('id', '=', 1)->rawValue('UPPER("foo")'); + $this->assertSame('BAR', $results); + } + public function testAggregateFunctions() { $builder = $this->getBuilder(); From 862187067e32ef04eeb8805349111cec4a2cea77 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 19 Oct 2022 10:03:37 -0500 Subject: [PATCH 2/2] formatting --- src/Illuminate/Database/Query/Builder.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index 99e8ed15511a..c7acc43e5f83 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -2605,9 +2605,9 @@ public function value($column) */ public function rawValue(string $expression, array $bindings = []) { - if ($result = $this->selectRaw($expression, $bindings)->take(1)->first()) { - return reset($result); - } + $result = (array) $this->selectRaw($expression, $bindings)->first(); + + return count($result) > 0 ? reset($result) : null; } /**