diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index 6beefbf6227..d5bf111a61a 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -225,7 +225,9 @@ public function orWhere($column, $operator = null, $value = null) */ public function hydrate(array $items) { - $instance = $this->model->newInstance(); + $instance = $this->model->newInstance()->setConnection( + $this->query->getConnection()->getName() + ); return $instance->newCollection(array_map(function ($item) use ($instance) { return $instance->newFromBuilder($item); @@ -459,8 +461,7 @@ public function get($columns = ['*']) public function getModels($columns = ['*']) { return $this->model->hydrate( - $this->query->get($columns)->all(), - $this->model->getConnectionName() + $this->query->get($columns)->all() )->all(); } diff --git a/tests/Database/DatabaseEloquentBuilderTest.php b/tests/Database/DatabaseEloquentBuilderTest.php index 1bee8ba62d8..63bc2345754 100755 --- a/tests/Database/DatabaseEloquentBuilderTest.php +++ b/tests/Database/DatabaseEloquentBuilderTest.php @@ -386,11 +386,10 @@ public function testGetModelsProperlyHydratesModels() $records[] = ['name' => 'taylor', 'age' => 26]; $records[] = ['name' => 'dayle', 'age' => 28]; $builder->getQuery()->shouldReceive('get')->once()->with(['foo'])->andReturn(new BaseCollection($records)); - $model = m::mock('Illuminate\Database\Eloquent\Model[getTable,getConnectionName,hydrate]'); + $model = m::mock('Illuminate\Database\Eloquent\Model[getTable,hydrate]'); $model->shouldReceive('getTable')->once()->andReturn('foo_table'); $builder->setModel($model); - $model->shouldReceive('getConnectionName')->once()->andReturn('foo_connection'); - $model->shouldReceive('hydrate')->once()->with($records, 'foo_connection')->andReturn(new Collection(['hydrated'])); + $model->shouldReceive('hydrate')->once()->with($records)->andReturn(new Collection(['hydrated'])); $models = $builder->getModels(['foo']); $this->assertEquals($models, ['hydrated']); diff --git a/tests/Database/DatabaseEloquentIntegrationTest.php b/tests/Database/DatabaseEloquentIntegrationTest.php index 33ca8553fe7..028b149572f 100644 --- a/tests/Database/DatabaseEloquentIntegrationTest.php +++ b/tests/Database/DatabaseEloquentIntegrationTest.php @@ -1061,6 +1061,14 @@ public function testBelongsToManyCustomPivot() $this->assertEquals('Jule Doe', $johnWithFriends->friends->find(4)->pivot->friend->name); } + public function testIsAfterRetrievingTheSameModel() + { + $saved = EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']); + $retrieved = EloquentTestUser::find(1); + + $this->assertTrue($saved->is($retrieved)); + } + /** * Helpers... */