Skip to content

Commit

Permalink
Allow table-qualified columns in pluck operations
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephSilber committed Nov 15, 2015
1 parent bce8cb7 commit 597a0af
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
25 changes: 6 additions & 19 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use InvalidArgumentException;
use Illuminate\Support\Collection;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Contracts\Support\Arrayable;
Expand Down Expand Up @@ -1543,11 +1542,9 @@ public function chunk($count, callable $callback)
*/
public function pluck($column, $key = null)
{
$columns = $this->getPluckSelect($column, $key);
$results = $this->get(func_get_args());

$results = new Collection($this->get($columns));

return $results->pluck($columns[0], Arr::get($columns, 1))->all();
return Arr::pluck($results, $this->stripTable($column), $this->stripTable($key));
}

/**
Expand All @@ -1565,24 +1562,14 @@ public function lists($column, $key = null)
}

/**
* Get the columns that should be used in a pluck select.
* Strip off the table name from a column identifier.
*
* @param string $column
* @param string $key
* @return array
* @return string
*/
protected function getPluckSelect($column, $key)
protected function stripTable($column)
{
$select = is_null($key) ? [$column] : [$column, $key];

// If the selected columns contain "dots", we will remove it so that the pluck
// operation can run normally. Specifying the table is not needed, since we
// really want the names of the columns as it is in this resulting array.
return array_map(function ($column) {
$dot = strpos($column, '.');

return $dot === false ? $column : substr($column, $dot + 1);
}, $select);
return is_null($column) ? $column : last(explode('.', $column));
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/Database/DatabaseEloquentIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,20 @@ public function testPluck()
$this->assertEquals([1 => '[email protected]', 2 => '[email protected]'], $keyed);
}

public function testPluckWithJoin()
{
$user1 = EloquentTestUser::create(['id' => 1, 'email' => '[email protected]']);
$user2 = EloquentTestUser::create(['id' => 2, 'email' => '[email protected]']);

$user2->posts()->create(['id' => 1, 'name' => 'First post']);
$user1->posts()->create(['id' => 2, 'name' => 'Second post']);

$query = EloquentTestUser::join('posts', 'users.id', '=', 'posts.user_id');

$this->assertEquals([1 => 'First post', 2 => 'Second post'], $query->pluck('name', 'posts.id')->all());
$this->assertEquals([2 => 'First post', 1 => 'Second post'], $query->pluck('name', 'users.id')->all());
}

public function testFindOrFail()
{
EloquentTestUser::create(['id' => 1, 'email' => '[email protected]']);
Expand Down

0 comments on commit 597a0af

Please sign in to comment.