diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphTo.php b/src/Illuminate/Database/Eloquent/Relations/MorphTo.php index caa6da522325..242c86d872c0 100644 --- a/src/Illuminate/Database/Eloquent/Relations/MorphTo.php +++ b/src/Illuminate/Database/Eloquent/Relations/MorphTo.php @@ -177,13 +177,11 @@ protected function getResultsByType($type) $key = $instance->getTable().'.'.$instance->getKeyName(); - $query = clone $this->query; + $query = $instance->newQuery(); $query->setEagerLoads($this->getEagerLoadsForInstance($instance)); - $query->setModel($instance); - - $query->useConnection($instance->getConnection()); + $this->mergeRelationWheresToMorphQuery($this->query, $query); return $query->whereIn($key, $this->gatherKeysByType($type)->all())->get(); } @@ -205,6 +203,22 @@ protected function getEagerLoadsForInstance(Model $instance) })->merge($instance->getEagerLoads())->all(); } + /** + * Merge the "wheres" from a relation query to a morph query. + * + * @param \Illuminate\Database\Eloquent\Builder $relationQuery + * @param \Illuminate\Database\Eloquent\Builder $morphQuery + * @return void + */ + protected function mergeRelationWheresToMorphQuery(Builder $relationQuery, Builder $morphQuery) + { + $removedScopes = $relationQuery->removedScopes(); + + $morphQuery->withoutGlobalScopes($removedScopes)->mergeWheres( + $relationQuery->getQuery()->wheres, $relationQuery->getBindings() + ); + } + /** * Gather all of the foreign keys for a given type. * diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index 68301b22e356..07c55a86fafe 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -2321,23 +2321,6 @@ public function useWritePdo() return $this; } - /** - * Use a different connection for the query. - * - * @param \Illuminate\Database\ConnectionInterface $connection - * @param \Illuminate\Database\Query\Grammars\Grammar $grammar - * @param \Illuminate\Database\Query\Processors\Processor $processor - * @return void - */ - public function useConnection(ConnectionInterface $connection, - Grammar $grammar = null, - Processor $processor = null) - { - $this->connection = $connection; - $this->grammar = $grammar ?: $connection->getQueryGrammar(); - $this->processor = $processor ?: $connection->getPostProcessor(); - } - /** * Handle dynamic method calls into the method. * diff --git a/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php b/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php index fe36f8c34686..cdb99130b678 100644 --- a/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php +++ b/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php @@ -579,6 +579,26 @@ public function testMorphToWithoutConstraints() $this->assertEquals(null, $comment->owner); } + public function testMorphToNonSoftDeletingModel() + { + $taylor = TestUserWithoutSoftDelete::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']); + $post1 = $taylor->posts()->create(['title' => 'First Title']); + $post1->comments()->create([ + 'body' => 'Comment Body', + 'owner_type' => TestUserWithoutSoftDelete::class, + 'owner_id' => $taylor->id, + ]); + + $comment = SoftDeletesTestCommentWithTrashed::with('owner')->first(); + + $this->assertEquals($taylor->email, $comment->owner->email); + + $taylor->delete(); + $comment = SoftDeletesTestCommentWithTrashed::with('owner')->first(); + + $this->assertEquals(null, $comment->owner); + } + /** * Helpers... */ @@ -611,6 +631,20 @@ protected function schema() } } +/** + * Eloquent Models... + */ +class TestUserWithoutSoftDelete extends Eloquent +{ + protected $table = 'users'; + protected $guarded = []; + + public function posts() + { + return $this->hasMany(SoftDeletesTestPost::class, 'user_id'); + } +} + /** * Eloquent Models... */