diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index 8f99e8e3..b63f8d6e 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -175,6 +175,19 @@ public function pluck($column) if ($result) return $result->{$column}; } + /** + * Get a single column's value from the first result of a query. + * + * @param string $column + * @return mixed + */ + public function value($column) + { + $result = $this->first(array($column)); + + if ($result) return $result->{$column}; + } + /** * Chunk the results of the query. * diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index d9deeda9..f4b84a72 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -1360,6 +1360,19 @@ public function pluck($column) return count($result) > 0 ? reset($result) : null; } + /** + * Get a single column's value from the first result of a query. + * + * @param string $column + * @return mixed + */ + public function value($column) + { + $result = (array) $this->first(array($column)); + + return count($result) > 0 ? reset($result) : null; + } + /** * Execute the query and get the first result. * diff --git a/tests/Database/DatabaseEloquentBuilderTest.php b/tests/Database/DatabaseEloquentBuilderTest.php index e409c93b..505947d6 100755 --- a/tests/Database/DatabaseEloquentBuilderTest.php +++ b/tests/Database/DatabaseEloquentBuilderTest.php @@ -157,6 +157,26 @@ public function testPluckMethodWithModelNotFound() } + public function testValueMethodWithModelFound() + { + $builder = m::mock('Illuminate\Database\Eloquent\Builder[first]', [$this->getMockQueryBuilder()]); + $mockModel = new StdClass; + $mockModel->name = 'foo'; + $builder->shouldReceive('first')->with(['name'])->andReturn($mockModel); + + $this->assertEquals('foo', $builder->value('name')); + } + + + public function testValueMethodWithModelNotFound() + { + $builder = m::mock('Illuminate\Database\Eloquent\Builder[first]', [$this->getMockQueryBuilder()]); + $builder->shouldReceive('first')->with(['name'])->andReturn(null); + + $this->assertNull($builder->value('name')); + } + + public function testChunkExecuteCallbackOverPaginatedRequest() { $builder = m::mock('Illuminate\Database\Eloquent\Builder[forPage,get]', [$this->getMockQueryBuilder()]); diff --git a/tests/Database/DatabaseQueryBuilderTest.php b/tests/Database/DatabaseQueryBuilderTest.php index bf0db305..e6e4c0f8 100755 --- a/tests/Database/DatabaseQueryBuilderTest.php +++ b/tests/Database/DatabaseQueryBuilderTest.php @@ -884,6 +884,19 @@ public function testPluckMethodReturnsSingleColumn(): void } + public function testValueMethodReturnsSingleColumn(): void + { + $builder = $this->getBuilder(); + $builder->getConnection()->shouldReceive('select')->once()->with('select "foo" from "users" where "id" = ? limit 1', [1] + )->andReturn([['foo' => 'bar']]); + $builder->getProcessor()->shouldReceive('processSelect')->once()->with($builder, [['foo' => 'bar']])->andReturn( + [['foo' => 'bar']] + ); + $results = $builder->from('users')->where('id', '=', 1)->value('foo'); + $this->assertEquals('bar', $results); + } + + public function testAggregateFunctions(): void { $builder = $this->getBuilder();