Skip to content

Commit

Permalink
Merge branch 'eusonlito/9.x' into 9.x
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Oct 19, 2022
2 parents 4335c5c + 8621870 commit 4a7bf43
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class Builder implements BuilderContract
'max',
'min',
'raw',
'rawValue',
'sum',
'toSql',
];
Expand Down
14 changes: 14 additions & 0 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [])
{
$result = (array) $this->selectRaw($expression, $bindings)->first();

return count($result) > 0 ? reset($result) : null;
}

/**
* Get a single column's value from the first result of a query if it's the sole matching record.
*
Expand Down
9 changes: 9 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 4a7bf43

Please sign in to comment.