Skip to content

Commit

Permalink
Throw exception for has() with MorphTo relationship (#25337)
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir authored and taylorotwell committed Aug 26, 2018
1 parent 4c0f7ab commit 4ea7c05
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions tests/Database/DatabaseEloquentIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down

0 comments on commit 4ea7c05

Please sign in to comment.