Skip to content

Commit 3ca7de5

Browse files
committed
Fixed eager loading with scoping
1 parent 0ded8b2 commit 3ca7de5

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

src/BaseRelation.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ abstract protected function relationExistenceCondition($hash, $table, $lft, $rgt
7979
public function getRelationExistenceQuery(EloquentBuilder $query, EloquentBuilder $parent,
8080
$columns = [ '*' ]
8181
) {
82-
$query = $this->getParent()->newInstance()->newScopedQuery()->select($columns);
82+
$query = $this->getParent()->replicate()->newScopedQuery()->select($columns);
8383

8484
$table = $query->getModel()->getTable();
8585

@@ -154,14 +154,16 @@ public function getResults()
154154
*/
155155
public function addEagerConstraints(array $models)
156156
{
157+
$model = reset($models);
158+
159+
$this->query = $model->newScopedQuery();
160+
157161
$this->query->whereNested(function (Builder $inner) use ($models) {
158162
// We will use this query in order to apply constraints to the
159163
// base query builder
160-
$outer = $this->parent->newScopedQuery();
164+
$outer = $this->parent->newQuery()->setQuery($inner);
161165

162166
foreach ($models as $model) {
163-
$outer->setQuery($inner);
164-
165167
$this->addEagerConstraint($outer, $model);
166168
}
167169
});

src/NodeTrait.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
use Illuminate\Database\Eloquent\Model;
88
use Illuminate\Database\Eloquent\Relations\BelongsTo;
99
use Illuminate\Database\Eloquent\Relations\HasMany;
10-
use Illuminate\Database\Eloquent\SoftDeletingScope;
11-
use Illuminate\Database\Query\Builder;
1210
use LogicException;
13-
use MongoDB\Driver\Query;
1411

1512
trait NodeTrait
1613
{
@@ -934,9 +931,7 @@ public function getPrevNode(array $columns = [ '*' ])
934931
*/
935932
public function getAncestors(array $columns = [ '*' ])
936933
{
937-
return $this->newScopedQuery()
938-
->defaultOrder()
939-
->ancestorsOf($this, $columns);
934+
return $this->ancestors()->get($columns);
940935
}
941936

942937
/**

tests/NodeTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,8 @@ public function testParentIdAttributeAccessorAppendsNode()
332332
$node->parent_id = null;
333333
$node->save();
334334

335+
$node->refreshNode();
336+
335337
$this->assertEquals(null, $node->parent_id);
336338
$this->assertTrue($node->isRoot());
337339
}

tests/ScopedNodeTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ public function testAncestors()
108108

109109
$this->assertEquals(1, $result->count());
110110
$this->assertEquals(2, $result->first()->getKey());
111+
112+
$node = MenuItem::with('ancestors')->find(5);
113+
114+
$result = $node->ancestors;
115+
116+
$this->assertEquals(1, $result->count());
117+
$this->assertEquals(2, $result->first()->getKey());
111118
}
112119

113120
public function testDepth()

0 commit comments

Comments
 (0)