Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
13 changes: 13 additions & 0 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
20 changes: 20 additions & 0 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()]);
Expand Down
13 changes: 13 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading