Skip to content

Commit

Permalink
Add pivot columns to graph children and parents relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir committed Dec 16, 2022
1 parent e441b5c commit 989fc0d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Eloquent/Traits/HasGraphAdjacencyList.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ public function children(): BelongsToMany
$this->getChildKeyName(),
$this->getLocalKeyName(),
$this->getLocalKeyName()
)->withPivot(
$this->getPivotColumns()
);
}

Expand Down Expand Up @@ -365,6 +367,8 @@ public function parents(): BelongsToMany
$this->getParentKeyName(),
$this->getLocalKeyName(),
$this->getLocalKeyName()
)->withPivot(
$this->getPivotColumns()
);
}

Expand Down
8 changes: 8 additions & 0 deletions tests/Graph/EloquentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public function testChildren()
$children = Node::find(1)->children;

$this->assertEquals([2, 3, 4, 5], $children->pluck('id')->all());
$this->assertEquals(
['parent_id' => 1, 'child_id' => 2, 'label' => 'a', 'weight' => 1, 'created_at' => $this->getFormattedTestNow()],
$children[0]->pivot->getAttributes()
);
}

public function testChildrenAndSelf()
Expand All @@ -60,6 +64,10 @@ public function testParents()
$parents = Node::find(5)->parents;

$this->assertEquals([1, 2, 10], $parents->pluck('id')->all());
$this->assertEquals(
['parent_id' => 1, 'child_id' => 5, 'label' => 'd', 'weight' => 4, 'created_at' => $this->getFormattedTestNow()],
$parents[0]->pivot->getAttributes()
);
}

public function testParentsAndSelf()
Expand Down

0 comments on commit 989fc0d

Please sign in to comment.