Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.6] Throw exception for has() with MorphTo relationship #25337

Merged
merged 1 commit into from
Aug 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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