Skip to content

Commit

Permalink
Fix eager loading and existence queries with SoftDeletes
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir committed Feb 24, 2019
1 parent ba76986 commit fc72b77
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/Relations/BelongsToThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ public function addConstraints()

$this->query->select([$this->getRelated()->getTable().'.*']);

$this->setSoftDeletes();

if (static::$constraints) {
$this->query->where($this->getQualifiedParentKeyName(), '=', $this->parent[$this->localKey]);

$this->query->whereNotNull($this->getQualifiedParentKeyName());

$this->setSoftDeletes();
}
}

Expand Down Expand Up @@ -275,7 +275,7 @@ public function getResults()
public function initRelation(array $models, $relation)
{
foreach ($models as $model) {
$model->setRelation($relation, $this->getRelated());
$model->setRelation($relation, null);
}

return $models;
Expand Down
4 changes: 3 additions & 1 deletion tests/BelongsToThroughTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function testEagerLoading()

$this->assertEquals(1, $comments[0]->country->id);
$this->assertEquals(2, $comments[1]->country->id);
$this->assertNull($comments[2]->country);
}

public function testLazyEagerLoading()
Expand All @@ -57,13 +58,14 @@ public function testLazyEagerLoading()

$this->assertEquals(1, $comments[0]->country->id);
$this->assertEquals(2, $comments[1]->country->id);
$this->assertNull($comments[2]->country);
}

public function testExistenceQuery()
{
$comments = Comment::has('country')->get();

$this->assertEquals([31, 32, 33], Arr::pluck($comments, 'id'));
$this->assertEquals([31, 32], Arr::pluck($comments, 'id'));
}

public function testExistenceQueryWithPrefix()
Expand Down

0 comments on commit fc72b77

Please sign in to comment.