diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index a7306ad15d03..7cbe32e9ad1c 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -7,6 +7,7 @@ use Illuminate\Support\Arr; use Illuminate\Support\Str; use Illuminate\Pagination\Paginator; +use Illuminate\Contracts\Support\Arrayable; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Database\Query\Builder as QueryBuilder; @@ -190,7 +191,7 @@ public function when($value, $callback, $default = null) */ public function whereKey($id) { - if (is_array($id)) { + if (is_array($id) || $id instanceof Arrayable) { $this->query->whereIn($this->model->getQualifiedKeyName(), $id); return $this; diff --git a/tests/Database/DatabaseEloquentBuilderTest.php b/tests/Database/DatabaseEloquentBuilderTest.php index 86d613e8ca3a..1bee8ba62d8f 100755 --- a/tests/Database/DatabaseEloquentBuilderTest.php +++ b/tests/Database/DatabaseEloquentBuilderTest.php @@ -805,6 +805,45 @@ public function testSelfHasNestedUsesAlias() $this->assertContains('"self_alias_hash"."id" = "self_related_stubs"."parent_id"', $sql); } + public function testWhereKeyMethodWithInt() + { + $model = $this->getMockModel(); + $builder = $this->getBuilder()->setModel($model); + $keyName = $model->getQualifiedKeyName(); + + $int = 1; + + $builder->getQuery()->shouldReceive('where')->once()->with($keyName, '=', $int); + + $builder->whereKey($int); + } + + public function testWhereKeyMethodWithArray() + { + $model = $this->getMockModel(); + $builder = $this->getBuilder()->setModel($model); + $keyName = $model->getQualifiedKeyName(); + + $array = [1, 2, 3]; + + $builder->getQuery()->shouldReceive('whereIn')->once()->with($keyName, $array); + + $builder->whereKey($array); + } + + public function testWhereKeyMethodWithCollection() + { + $model = $this->getMockModel(); + $builder = $this->getBuilder()->setModel($model); + $keyName = $model->getQualifiedKeyName(); + + $collection = new Collection([1, 2, 3]); + + $builder->getQuery()->shouldReceive('whereIn')->once()->with($keyName, $collection); + + $builder->whereKey($collection); + } + protected function mockConnectionForModel($model, $database) { $grammarClass = 'Illuminate\Database\Query\Grammars\\'.$database.'Grammar';