Skip to content

Commit

Permalink
Inline Array::pluck method
Browse files Browse the repository at this point in the history
This prepares for the final step of removing (without replacement)
the expensive parts of its implementation (from which, I suspect,
the code will get shorter again).
  • Loading branch information
franzliedke committed Mar 10, 2018
1 parent 1f9f2b1 commit 374db82
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1989,7 +1989,7 @@ protected function enforceOrderBy()
*/
public function pluck($column, $key = null)
{
$results = $this->onceWithColumns(
$queryResult = $this->onceWithColumns(
is_null($key) ? [$column] : [$column, $key],
function () {
return $this->processor->processSelect($this, $this->runSelect());
Expand All @@ -1999,13 +1999,28 @@ function () {
// If the columns are qualified with a table or have an alias, we cannot use
// those directly in the "pluck" operations since the results from the DB
// are only keyed by the column itself. We'll strip the table out here.
return collect(
Arr::pluck(
$results,
$this->stripTableForPluck($column),
$this->stripTableForPluck($key)
)
);
$column = $this->stripTableForPluck($column);
$key = $this->stripTableForPluck($key);

$results = [];

foreach ($queryResult as $row) {
$itemValue = data_get($row, $column);

if (is_null($key)) {
$results[] = $itemValue;
} else {
$itemKey = data_get($row, $key);

if (is_object($itemKey) && method_exists($itemKey, '__toString')) {
$itemKey = (string) $itemKey;
}

$results[$itemKey] = $itemValue;
}
}

return collect($results);
}

/**
Expand Down

0 comments on commit 374db82

Please sign in to comment.