diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index 8f99e8e35..a3c75ade5 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -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)); @@ -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. * diff --git a/src/Illuminate/Database/Eloquent/Model.php b/src/Illuminate/Database/Eloquent/Model.php index 9f0cd7015..f0cf26986 100755 --- a/src/Illuminate/Database/Eloquent/Model.php +++ b/src/Illuminate/Database/Eloquent/Model.php @@ -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; @@ -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. @@ -265,11 +265,11 @@ 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; } @@ -277,7 +277,7 @@ public static function addGlobalScope(ScopeInterface $scope): void /** * Determine if a model has a global scope. * - * @param ScopeInterface $scope + * @param Scope $scope * * @return bool */ @@ -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) { @@ -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 { @@ -1740,7 +1740,7 @@ public function newQuery() /** * Get a new query instance without a given scope. * - * @param ScopeInterface $scope + * @param Scope $scope * * @return Builder */ @@ -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(); } @@ -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; } /** diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php index 1b66d6ba0..df1a2d2c4 100755 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php @@ -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()); } /** @@ -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); diff --git a/src/Illuminate/Database/Eloquent/ScopeInterface.php b/src/Illuminate/Database/Eloquent/Scope.php similarity index 94% rename from src/Illuminate/Database/Eloquent/ScopeInterface.php rename to src/Illuminate/Database/Eloquent/Scope.php index b0a93a90c..da1173af1 100644 --- a/src/Illuminate/Database/Eloquent/ScopeInterface.php +++ b/src/Illuminate/Database/Eloquent/Scope.php @@ -1,6 +1,6 @@ table()->lists('migration'); + return $this->table()->pluck('migration'); } /** diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index d9deeda95..2bb7b8b3e 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -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)); @@ -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. * @@ -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)); } /** diff --git a/tests/Database/DatabaseEloquentBelongsToManyTest.php b/tests/Database/DatabaseEloquentBelongsToManyTest.php index 62220d512..cd0183a91 100755 --- a/tests/Database/DatabaseEloquentBelongsToManyTest.php +++ b/tests/Database/DatabaseEloquentBelongsToManyTest.php @@ -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); @@ -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'] @@ -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'] @@ -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]); @@ -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]); @@ -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 => []] ); diff --git a/tests/Database/DatabaseEloquentBuilderTest.php b/tests/Database/DatabaseEloquentBuilderTest.php index e409c93bc..1dfec3f21 100755 --- a/tests/Database/DatabaseEloquentBuilderTest.php +++ b/tests/Database/DatabaseEloquentBuilderTest.php @@ -137,23 +137,23 @@ public function testGetMethodDoesntHydrateEagerRelationsWhenNoResultsAreReturned } - public function testPluckMethodWithModelFound() +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->pluck('name')); + $this->assertEquals('foo', $builder->value('name')); } - public function testPluckMethodWithModelNotFound() + public function testValueMethodWithModelNotFound() { $builder = m::mock('Illuminate\Database\Eloquent\Builder[first]', [$this->getMockQueryBuilder()]); $builder->shouldReceive('first')->with(['name'])->andReturn(null); - $this->assertNull($builder->pluck('name')); + $this->assertNull($builder->value('name')); } @@ -181,7 +181,7 @@ public function testChunkExecuteCallbackOverPaginatedRequest() public function testListsReturnsTheMutatedAttributesOfAModel() { $builder = $this->getBuilder(); - $builder->getQuery()->shouldReceive('lists')->with('name', '')->andReturn(['bar', 'baz']); + $builder->getQuery()->shouldReceive('pluck')->with('name', '')->andReturn(['bar', 'baz']); $builder->setModel($this->getMockModel()); $builder->getModel()->shouldReceive('hasGetMutator')->with('name')->andReturn(true); $builder->getModel()->shouldReceive('newFromBuilder')->with(['name' => 'bar'])->andReturn(new EloquentBuilderTestListsStub( @@ -191,18 +191,18 @@ public function testListsReturnsTheMutatedAttributesOfAModel() ['name' => 'baz'] )); - $this->assertEquals(['foo_bar', 'foo_baz'], $builder->lists('name')); + $this->assertEquals(['foo_bar', 'foo_baz'], $builder->pluck('name')); } public function testListsWithoutModelGetterJustReturnTheAttributesFoundInDatabase() { $builder = $this->getBuilder(); - $builder->getQuery()->shouldReceive('lists')->with('name', '')->andReturn(['bar', 'baz']); + $builder->getQuery()->shouldReceive('pluck')->with('name', '')->andReturn(['bar', 'baz']); $builder->setModel($this->getMockModel()); $builder->getModel()->shouldReceive('hasGetMutator')->with('name')->andReturn(false); - $this->assertEquals(['bar', 'baz'], $builder->lists('name')); + $this->assertEquals(['bar', 'baz'], $builder->pluck('name')); } @@ -589,7 +589,7 @@ public function scopeApproved($query) } class EloquentBuilderTestWithTrashedStub extends Illuminate\Database\Eloquent\Model { - use Illuminate\Database\Eloquent\SoftDeletingTrait; + use Illuminate\Database\Eloquent\SoftDeletes; protected string $table = 'table'; #[\Override] public function getKeyName(): string { return 'foo'; } @@ -597,7 +597,7 @@ public function getKeyName(): string { return 'foo'; } class EloquentBuilderTestNestedStub extends Illuminate\Database\Eloquent\Model { protected string $table = 'table'; - use Illuminate\Database\Eloquent\SoftDeletingTrait; + use Illuminate\Database\Eloquent\SoftDeletes; } class EloquentBuilderTestListsStub { diff --git a/tests/Database/DatabaseMigrationRepositoryTest.php b/tests/Database/DatabaseMigrationRepositoryTest.php index eaed40d4a..7f289c7e7 100755 --- a/tests/Database/DatabaseMigrationRepositoryTest.php +++ b/tests/Database/DatabaseMigrationRepositoryTest.php @@ -22,7 +22,7 @@ public function testGetRanMigrationsListMigrationsByPackage() $connectionMock = m::mock(Connection::class); $repo->getConnectionResolver()->shouldReceive('connection')->with(null)->andReturn($connectionMock); $repo->getConnection()->shouldReceive('table')->once()->with('migrations')->andReturn($query); - $query->shouldReceive('lists')->once()->with('migration')->andReturn('bar'); + $query->shouldReceive('pluck')->once()->with('migration')->andReturn('bar'); $this->assertEquals('bar', $repo->getRan()); } diff --git a/tests/Database/DatabaseQueryBuilderTest.php b/tests/Database/DatabaseQueryBuilderTest.php index bf0db305a..ffc73c47c 100755 --- a/tests/Database/DatabaseQueryBuilderTest.php +++ b/tests/Database/DatabaseQueryBuilderTest.php @@ -750,7 +750,7 @@ public function testListMethodsGetsArrayOfColumnValues(): void { return $results; }); - $results = $builder->from('users')->where('id', '=', 1)->lists('foo'); + $results = $builder->from('users')->where('id', '=', 1)->pluck('foo'); $this->assertEquals(['bar', 'baz'], $results); $builder = $this->getBuilder(); @@ -762,7 +762,7 @@ public function testListMethodsGetsArrayOfColumnValues(): void { return $results; }); - $results = $builder->from('users')->where('id', '=', 1)->lists('foo', 'id'); + $results = $builder->from('users')->where('id', '=', 1)->pluck('foo', 'id'); $this->assertEquals([1 => 'bar', 10 => 'baz'], $results); } @@ -871,7 +871,7 @@ public function testQuickPaginateCorrectlyCreatesPaginatorInstance(): void } - 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] @@ -879,7 +879,7 @@ public function testPluckMethodReturnsSingleColumn(): void $builder->getProcessor()->shouldReceive('processSelect')->once()->with($builder, [['foo' => 'bar']])->andReturn( [['foo' => 'bar']] ); - $results = $builder->from('users')->where('id', '=', 1)->pluck('foo'); + $results = $builder->from('users')->where('id', '=', 1)->value('foo'); $this->assertEquals('bar', $results); } diff --git a/tests/Database/DatabaseSoftDeletingTraitTest.php b/tests/Database/DatabaseSoftDeletingTraitTest.php index 82d144479..f3ce3e492 100644 --- a/tests/Database/DatabaseSoftDeletingTraitTest.php +++ b/tests/Database/DatabaseSoftDeletingTraitTest.php @@ -54,7 +54,7 @@ public function testRestoreCancel() class DatabaseSoftDeletingTraitStub { - use Illuminate\Database\Eloquent\SoftDeletingTrait; + use Illuminate\Database\Eloquent\SoftDeletes; public $deleted_at; public function newQuery() {