Skip to content

Commit 47252e1

Browse files
committed
Allow table-qualified columns in pluck operations
1 parent c6b1dcc commit 47252e1

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

src/Illuminate/Database/Query/Builder.php

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Illuminate\Support\Arr;
88
use Illuminate\Support\Str;
99
use InvalidArgumentException;
10-
use Illuminate\Support\Collection;
1110
use Illuminate\Pagination\Paginator;
1211
use Illuminate\Support\Traits\Macroable;
1312
use Illuminate\Contracts\Support\Arrayable;
@@ -1543,11 +1542,9 @@ public function chunk($count, callable $callback)
15431542
*/
15441543
public function pluck($column, $key = null)
15451544
{
1546-
$columns = $this->getPluckSelect($column, $key);
1545+
$results = $this->get(func_get_args());
15471546

1548-
$results = new Collection($this->get($columns));
1549-
1550-
return $results->pluck($columns[0], Arr::get($columns, 1))->all();
1547+
return Arr::pluck($results, $this->stripTable($column), $this->stripTable($key));
15511548
}
15521549

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

15671564
/**
1568-
* Get the columns that should be used in a pluck select.
1565+
* Strip off the table name from a column identifier.
15691566
*
15701567
* @param string $column
1571-
* @param string $key
1572-
* @return array
1568+
* @return string
15731569
*/
1574-
protected function getPluckSelect($column, $key)
1570+
protected function stripTable($column)
15751571
{
1576-
$select = is_null($key) ? [$column] : [$column, $key];
1577-
1578-
// If the selected columns contain "dots", we will remove it so that the pluck
1579-
// operation can run normally. Specifying the table is not needed, since we
1580-
// really want the names of the columns as it is in this resulting array.
1581-
return array_map(function ($column) {
1582-
$dot = strpos($column, '.');
1583-
1584-
return $dot === false ? $column : substr($column, $dot + 1);
1585-
}, $select);
1572+
return is_null($column) ? $column : last(explode('.', $column));
15861573
}
15871574

15881575
/**

tests/Database/DatabaseEloquentIntegrationTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,20 @@ public function testPluck()
179179
$this->assertEquals([1 => 'taylorotwell@gmail.com', 2 => 'abigailotwell@gmail.com'], $keyed);
180180
}
181181

182+
public function testPluckWithJoin()
183+
{
184+
$user1 = EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
185+
$user2 = EloquentTestUser::create(['id' => 2, 'email' => 'abigailotwell@gmail.com']);
186+
187+
$user2->posts()->create(['id' => 1, 'name' => 'First post']);
188+
$user1->posts()->create(['id' => 2, 'name' => 'Second post']);
189+
190+
$query = EloquentTestUser::join('posts', 'users.id', '=', 'posts.user_id');
191+
192+
$this->assertEquals([1 => 'First post', 2 => 'Second post'], $query->pluck('name', 'posts.id')->all());
193+
$this->assertEquals([2 => 'First post', 1 => 'Second post'], $query->pluck('name', 'users.id')->all());
194+
}
195+
182196
public function testFindOrFail()
183197
{
184198
EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);

0 commit comments

Comments
 (0)