From 6fda583fee5d9a0e8236276661e551b8eda3b630 Mon Sep 17 00:00:00 2001 From: Jonas Staudenmeir Date: Sun, 26 Aug 2018 02:51:14 +0200 Subject: [PATCH] Throw exception for has() with MorphTo relationship --- .../Database/Eloquent/Concerns/QueriesRelationships.php | 6 ++++++ tests/Database/DatabaseEloquentIntegrationTest.php | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php b/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php index 5f2ec2dab7b9..08e5323ecaf7 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php +++ b/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php @@ -3,9 +3,11 @@ namespace Illuminate\Database\Eloquent\Concerns; use Closure; +use RuntimeException; use Illuminate\Support\Str; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Query\Expression; +use Illuminate\Database\Eloquent\Relations\MorphTo; use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Database\Query\Builder as QueryBuilder; @@ -29,6 +31,10 @@ public function has($relation, $operator = '>=', $count = 1, $boolean = 'and', C $relation = $this->getRelationWithoutConstraints($relation); + if ($relation instanceof MorphTo) { + throw new RuntimeException('has() and whereHas() do not support MorphTo relationships.'); + } + // If we only need to check for the existence of the relation, then we can optimize // the subquery to only run a "where exists" clause instead of this full "count" // clause. This will make these queries run much faster compared with a count. diff --git a/tests/Database/DatabaseEloquentIntegrationTest.php b/tests/Database/DatabaseEloquentIntegrationTest.php index f853ae8531e0..c0c22ce1c17a 100644 --- a/tests/Database/DatabaseEloquentIntegrationTest.php +++ b/tests/Database/DatabaseEloquentIntegrationTest.php @@ -717,6 +717,14 @@ public function testHasWithNonWhereBindings() $this->assertEquals($questionMarksCount, $bindingsCount); } + /** + * @expectedException \RuntimeException + */ + public function testHasOnMorphToRelationship() + { + EloquentTestPhoto::has('imageable')->get(); + } + public function testBelongsToManyRelationshipModelsAreProperlyHydratedOverChunkedRequest() { $user = EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']);