Skip to content
Open
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
58 changes: 29 additions & 29 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,39 @@ public function get($columns = array('*'))
}

/**
* Pluck a single column from the database.
* Get an array with the values of a given column.
*
* @param string $column
* @param string $key
* @return array
*/
public function pluck($column, $key = null)
{
$results = $this->query->pluck($column, $key);

// If the model has a mutator for the requested column, we will spin through
// the results and mutate the values so that the mutated version of these
// columns are returned as you would expect from these Eloquent models.
if ($this->model->hasGetMutator($column))
{
foreach ($results as $key => &$value)
{
$fill = array($column => $value);

$value = $this->model->newFromBuilder($fill)->$column;
}
}

return $results;
}

/**
* Get a single column's value from the first result of a query.
*
* @param string $column
* @return mixed
*/
public function pluck($column)
public function value($column)
{
$result = $this->first(array($column));

Expand Down Expand Up @@ -199,33 +226,6 @@ public function chunk($count, callable $callback)
}
}

/**
* Get an array with the values of a given column.
*
* @param string $column
* @param string $key
* @return array
*/
public function lists($column, $key = null)
{
$results = $this->query->lists($column, $key);

// If the model has a mutator for the requested column, we will spin through
// the results and mutate the values so that the mutated version of these
// columns are returned as you would expect from these Eloquent models.
if ($this->model->hasGetMutator($column))
{
foreach ($results as $key => &$value)
{
$fill = array($column => $value);

$value = $this->model->newFromBuilder($fill)->$column;
}
}

return $results;
}

/**
* Get a paginator for the "select" statement.
*
Expand Down
26 changes: 13 additions & 13 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Contracts\JsonableInterface;
use Illuminate\Support\Contracts\ArrayableInterface;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\Relations\MorphMany;
Expand All @@ -27,7 +27,7 @@
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\ConnectionResolverInterface as Resolver;

abstract class Model implements ArrayAccess, ArrayableInterface, JsonableInterface, JsonSerializable {
abstract class Model implements ArrayAccess, Arrayable, Jsonable, JsonSerializable {

/**
* The connection name for the model.
Expand Down Expand Up @@ -265,19 +265,19 @@ protected static function bootTraits(): void
/**
* Register a new global scope on the model.
*
* @param ScopeInterface $scope
* @param Scope $scope
*
* @return void
*/
public static function addGlobalScope(ScopeInterface $scope): void
public static function addGlobalScope(Scope $scope): void
{
static::$globalScopes[get_called_class()][get_class($scope)] = $scope;
}

/**
* Determine if a model has a global scope.
*
* @param ScopeInterface $scope
* @param Scope $scope
*
* @return bool
*/
Expand All @@ -289,11 +289,11 @@ public static function hasGlobalScope($scope): bool
/**
* Get a global scope registered with the model.
*
* @param ScopeInterface $scope
* @param Scope $scope
*
* @return ScopeInterface|null
* @return Scope|null
*/
public static function getGlobalScope($scope): ?ScopeInterface
public static function getGlobalScope($scope): ?Scope
{
return Arr::first(static::$globalScopes[get_called_class()], function($value, $key) use ($scope)
{
Expand All @@ -304,7 +304,7 @@ public static function getGlobalScope($scope): ?ScopeInterface
/**
* Get the global scopes for this class instance.
*
* @return ScopeInterface[]
* @return Scope[]
*/
public function getGlobalScopes(): array
{
Expand Down Expand Up @@ -1740,7 +1740,7 @@ public function newQuery()
/**
* Get a new query instance without a given scope.
*
* @param ScopeInterface $scope
* @param Scope $scope
*
* @return Builder
*/
Expand Down Expand Up @@ -2317,7 +2317,7 @@ public function relationsToArray():array
// If the values implements the Arrayable interface we can just call this
// toArray method on the instances which will convert both models and
// collections to their proper array form and we'll set the values.
if ($value instanceof ArrayableInterface)
if ($value instanceof Arrayable)
{
$relation = $value->toArray();
}
Expand Down Expand Up @@ -2515,7 +2515,7 @@ protected function mutateAttributeForArray($key, $value)
{
$value = $this->mutateAttribute($key, $value);

return $value instanceof ArrayableInterface ? $value->toArray() : $value;
return $value instanceof Arrayable ? $value->toArray() : $value;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ public function getRelatedIds()

$fullKey = $related->getQualifiedKeyName();

return $this->getQuery()->select($fullKey)->lists($related->getKeyName());
return $this->getQuery()->select($fullKey)->pluck($related->getKeyName());
}

/**
Expand Down Expand Up @@ -596,7 +596,7 @@ public function sync($ids, $detaching = true)
// First we need to attach any of the associated models that are not currently
// in this joining table. We'll spin through the given IDs, checking to see
// if they exist in the array of current ones, and if not we will insert.
$current = $this->newPivotQuery()->lists($this->otherKey);
$current = $this->newPivotQuery()->pluck($this->otherKey);

$records = $this->formatSyncList($ids);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php namespace Illuminate\Database\Eloquent;

interface ScopeInterface {
interface Scope {

/**
* Apply the scope to a given Eloquent query builder.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php namespace Illuminate\Database\Eloquent;

trait SoftDeletingTrait {
trait SoftDeletes {

/**
* Indicates if the model is currently force deleting.
Expand All @@ -14,7 +14,7 @@ trait SoftDeletingTrait {
*
* @return void
*/
public static function bootSoftDeletingTrait()
public static function bootSoftDeletes()
{
static::addGlobalScope(new SoftDeletingScope);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/SoftDeletingScope.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php namespace Illuminate\Database\Eloquent;

class SoftDeletingScope implements ScopeInterface {
class SoftDeletingScope implements Scope {

/**
* All of the extensions to be added to the builder.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function __construct(Resolver $resolver, $table)
*/
public function getRan()
{
return $this->table()->lists('migration');
return $this->table()->pluck('migration');
}

/**
Expand Down
70 changes: 35 additions & 35 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1348,12 +1348,43 @@ public function find($id, $columns = array('*'))
}

/**
* Pluck a single column's value from the first result of a query.
* Get an array with the values of a given column.
*
* @param string $column
* @param string $key
* @return array
*/
public function pluck($column, $key = null)
{
$columns = $this->getListSelect($column, $key);

// First we will just get all of the column values for the record result set
// then we can associate those values with the column if it was specified
// otherwise we can just give these values back without a specific key.
$results = new Collection($this->get($columns));

$values = $results->fetch($columns[0])->all();

// If a key was specified and we have results, we will go ahead and combine
// the values with the keys of all of the records so that the values can
// be accessed by the key of the rows instead of simply being numeric.
if ( ! is_null($key) && count($results) > 0)
{
$keys = $results->fetch($key)->all();

return array_combine($keys, $values);
}

return $values;
}

/**
* Get a single column's value from the first result of a query.
*
* @param string $column
* @return mixed
*/
public function pluck($column)
public function value($column)
{
$result = (array) $this->first(array($column));

Expand Down Expand Up @@ -1616,37 +1647,6 @@ private function defaultKeyName(): string
return 'id';
}

/**
* Get an array with the values of a given column.
*
* @param string $column
* @param string $key
* @return array
*/
public function lists($column, $key = null)
{
$columns = $this->getListSelect($column, $key);

// First we will just get all of the column values for the record result set
// then we can associate those values with the column if it was specified
// otherwise we can just give these values back without a specific key.
$results = new Collection($this->get($columns));

$values = $results->fetch($columns[0])->all();

// If a key was specified and we have results, we will go ahead and combine
// the values with the keys of all of the records so that the values can
// be accessed by the key of the rows instead of simply being numeric.
if ( ! is_null($key) && count($results) > 0)
{
$keys = $results->fetch($key)->all();

return array_combine($keys, $values);
}

return $values;
}

/**
* Get the columns that should be used in a list array.
*
Expand Down Expand Up @@ -1678,9 +1678,9 @@ protected function getListSelect($column, $key)
*/
public function implode($column, $glue = null)
{
if (is_null($glue)) return implode($this->lists($column));
if (is_null($glue)) return implode($this->pluck($column));

return implode($glue, $this->lists($column));
return implode($glue, $this->pluck($column));
}

/**
Expand Down
12 changes: 6 additions & 6 deletions tests/Database/DatabaseEloquentBelongsToManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ public function testSyncMethodSyncsIntermediateTableWithGivenArray($list)
$query->shouldReceive('where')->once()->with('user_id', 1)->andReturn($query);
$relation->getQuery()->shouldReceive('getQuery')->andReturn($mockQueryBuilder = m::mock('StdClass'));
$mockQueryBuilder->shouldReceive('newQuery')->once()->andReturn($query);
$query->shouldReceive('lists')->once()->with('role_id')->andReturn([1, 2, 3]);
$query->shouldReceive('pluck')->once()->with('role_id')->andReturn([1, 2, 3]);
$relation->expects($this->once())->method('attach')->with($this->equalTo(4), $this->equalTo([]), $this->equalTo(false));
$relation->expects($this->once())->method('detach')->with($this->equalTo([1]));
$relation->getRelated()->shouldReceive('touches')->andReturn(false);
Expand All @@ -330,7 +330,7 @@ public function testSyncMethodSyncsIntermediateTableWithGivenArrayAndAttributes(
$query->shouldReceive('where')->once()->with('user_id', 1)->andReturn($query);
$relation->getQuery()->shouldReceive('getQuery')->andReturn($mockQueryBuilder = m::mock('StdClass'));
$mockQueryBuilder->shouldReceive('newQuery')->once()->andReturn($query);
$query->shouldReceive('lists')->once()->with('role_id')->andReturn([1, 2, 3]);
$query->shouldReceive('pluck')->once()->with('role_id')->andReturn([1, 2, 3]);
$relation->expects($this->once())->method('attach')->with($this->equalTo(4), $this->equalTo(['foo' => 'bar']), $this->equalTo(false));
$relation->expects($this->once())->method('updateExistingPivot')->with($this->equalTo(3), $this->equalTo(
['baz' => 'qux']
Expand All @@ -355,7 +355,7 @@ public function testSyncMethodDoesntReturnValuesThatWereNotUpdated()
$query->shouldReceive('where')->once()->with('user_id', 1)->andReturn($query);
$relation->getQuery()->shouldReceive('getQuery')->andReturn($mockQueryBuilder = m::mock('StdClass'));
$mockQueryBuilder->shouldReceive('newQuery')->once()->andReturn($query);
$query->shouldReceive('lists')->once()->with('role_id')->andReturn([1, 2, 3]);
$query->shouldReceive('pluck')->once()->with('role_id')->andReturn([1, 2, 3]);
$relation->expects($this->once())->method('attach')->with($this->equalTo(4), $this->equalTo(['foo' => 'bar']), $this->equalTo(false));
$relation->expects($this->once())->method('updateExistingPivot')->with($this->equalTo(3), $this->equalTo(
['baz' => 'qux']
Expand All @@ -380,7 +380,7 @@ public function testTouchMethodSyncsTimestamps()
$relation->getRelated()->shouldReceive('freshTimestamp')->andReturn($carbon);
$relation->getRelated()->shouldReceive('getQualifiedKeyName')->andReturn('table.id');
$relation->getQuery()->shouldReceive('select')->once()->with('table.id')->andReturn($relation->getQuery());
$relation->getQuery()->shouldReceive('lists')->once()->with('id')->andReturn([1, 2, 3]);
$relation->getQuery()->shouldReceive('pluck')->once()->with('id')->andReturn([1, 2, 3]);
$relation->getRelated()->shouldReceive('newQuery')->once()->andReturn($query = m::mock(Builder::class));
$query->shouldReceive('whereIn')->once()->with('id', [1, 2, 3])->andReturn($query);
$query->shouldReceive('update')->once()->with(['updated_at' => $carbon]);
Expand Down Expand Up @@ -409,7 +409,7 @@ public function testSyncMethodConvertsCollectionToArrayOfKeys()
$query->shouldReceive('where')->once()->with('user_id', 1)->andReturn($query);
$relation->getQuery()->shouldReceive('getQuery')->andReturn($mockQueryBuilder = m::mock('StdClass'));
$mockQueryBuilder->shouldReceive('newQuery')->once()->andReturn($query);
$query->shouldReceive('lists')->once()->with('role_id')->andReturn([1, 2, 3]);
$query->shouldReceive('pluck')->once()->with('role_id')->andReturn([1, 2, 3]);

$collection = m::mock(Collection::class);
$collection->shouldReceive('modelKeys')->once()->andReturn([1, 2, 3]);
Expand Down Expand Up @@ -443,7 +443,7 @@ public function testWherePivotParamsUsedForNewQueries()
$query->shouldReceive('where')->once()->with('foo', '=', 'bar')->andReturn($query);

// This is so $relation->sync() works
$query->shouldReceive('lists')->once()->with('role_id')->andReturn([1, 2, 3]);
$query->shouldReceive('pluck')->once()->with('role_id')->andReturn([1, 2, 3]);
$relation->expects($this->once())->method('formatSyncList')->with([1, 2, 3])->willReturn(
[1 => [], 2 => [], 3 => []]
);
Expand Down
Loading
Loading