Skip to content

Commit 4a7bf43

Browse files
committed
Merge branch 'eusonlito/9.x' into 9.x
2 parents 4335c5c + 8621870 commit 4a7bf43

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

src/Illuminate/Database/Eloquent/Builder.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ class Builder implements BuilderContract
110110
'max',
111111
'min',
112112
'raw',
113+
'rawValue',
113114
'sum',
114115
'toSql',
115116
];

src/Illuminate/Database/Query/Builder.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2596,6 +2596,20 @@ public function value($column)
25962596
return count($result) > 0 ? reset($result) : null;
25972597
}
25982598

2599+
/**
2600+
* Get a single expression value from the first result of a query.
2601+
*
2602+
* @param string $expression
2603+
* @param array $bindings
2604+
* @return mixed
2605+
*/
2606+
public function rawValue(string $expression, array $bindings = [])
2607+
{
2608+
$result = (array) $this->selectRaw($expression, $bindings)->first();
2609+
2610+
return count($result) > 0 ? reset($result) : null;
2611+
}
2612+
25992613
/**
26002614
* Get a single column's value from the first result of a query if it's the sole matching record.
26012615
*

tests/Database/DatabaseQueryBuilderTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2464,6 +2464,15 @@ public function testValueMethodReturnsSingleColumn()
24642464
$this->assertSame('bar', $results);
24652465
}
24662466

2467+
public function testRawValueMethodReturnsSingleColumn()
2468+
{
2469+
$builder = $this->getBuilder();
2470+
$builder->getConnection()->shouldReceive('select')->once()->with('select UPPER("foo") from "users" where "id" = ? limit 1', [1], true)->andReturn([['UPPER("foo")' => 'BAR']]);
2471+
$builder->getProcessor()->shouldReceive('processSelect')->once()->with($builder, [['UPPER("foo")' => 'BAR']])->andReturn([['UPPER("foo")' => 'BAR']]);
2472+
$results = $builder->from('users')->where('id', '=', 1)->rawValue('UPPER("foo")');
2473+
$this->assertSame('BAR', $results);
2474+
}
2475+
24672476
public function testAggregateFunctions()
24682477
{
24692478
$builder = $this->getBuilder();

0 commit comments

Comments
 (0)