From 32d03527163a3edd7f88e4b74b03575e4bdb5db7 Mon Sep 17 00:00:00 2001 From: Jonas Staudenmeir Date: Thu, 19 Aug 2021 20:23:06 +0200 Subject: [PATCH] Add getThroughParents() method --- src/Relations/BelongsToThrough.php | 10 ++++++++++ tests/BelongsToThroughTest.php | 11 +++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/Relations/BelongsToThrough.php b/src/Relations/BelongsToThrough.php index 10e8f9f..0ea91f9 100644 --- a/src/Relations/BelongsToThrough.php +++ b/src/Relations/BelongsToThrough.php @@ -295,6 +295,16 @@ public function withTrashed(...$columns) return $this; } + /** + * Get the "through" parent model instances. + * + * @return \Illuminate\Database\Eloquent\Model[] + */ + public function getThroughParents() + { + return $this->throughParents; + } + /** * Get the foreign key for the first "through" parent model. * diff --git a/tests/BelongsToThroughTest.php b/tests/BelongsToThroughTest.php index ab62a3a..398d42e 100644 --- a/tests/BelongsToThroughTest.php +++ b/tests/BelongsToThroughTest.php @@ -4,6 +4,8 @@ use Tests\Models\Comment; use Tests\Models\Country; +use Tests\Models\Post; +use Tests\Models\User; class BelongsToThroughTest extends TestCase { @@ -116,4 +118,13 @@ public function testWithTrashedIntermediate() $this->assertEquals(3, $country->id); } + + public function testGetThroughParents() + { + $throughParents = Comment::first()->country()->getThroughParents(); + + $this->assertCount(2, $throughParents); + $this->assertInstanceOf(User::class, $throughParents[0]); + $this->assertInstanceOf(Post::class, $throughParents[1]); + } }