Skip to content

Commit

Permalink
Add getThroughParents() method
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir committed Aug 19, 2021
1 parent c5f16cb commit 32d0352
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Relations/BelongsToThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
11 changes: 11 additions & 0 deletions tests/BelongsToThroughTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Tests\Models\Comment;
use Tests\Models\Country;
use Tests\Models\Post;
use Tests\Models\User;

class BelongsToThroughTest extends TestCase
{
Expand Down Expand Up @@ -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]);
}
}

0 comments on commit 32d0352

Please sign in to comment.