diff --git a/src/Relations/BelongsToThrough.php b/src/Relations/BelongsToThrough.php index 56f0209..f2f2649 100644 --- a/src/Relations/BelongsToThrough.php +++ b/src/Relations/BelongsToThrough.php @@ -5,12 +5,15 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\Concerns\SupportsDefaultModels; use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Support\Str; class BelongsToThrough extends Relation { + use SupportsDefaultModels; + /** * The column alias for the local key on the first "through" parent model. * @@ -153,7 +156,7 @@ public function addEagerConstraints(array $models) public function initRelation(array $models, $relation) { foreach ($models as $model) { - $model->setRelation($relation, null); + $model->setRelation($relation, $this->getDefaultFor($model)); } return $models; @@ -208,7 +211,7 @@ protected function buildDictionary(Collection $results) */ public function getResults() { - return $this->first(); + return $this->first() ?: $this->getDefaultFor($this->parent); } /** diff --git a/tests/BelongsToThroughTest.php b/tests/BelongsToThroughTest.php index e88e974..c0cfcaa 100644 --- a/tests/BelongsToThroughTest.php +++ b/tests/BelongsToThroughTest.php @@ -3,6 +3,7 @@ namespace Tests; use Tests\Models\Comment; +use Tests\Models\Country; class BelongsToThroughTest extends TestCase { @@ -38,7 +39,15 @@ public function testLazyLoadingWithSoftDeletes() { $country = Comment::find(33)->country; - $this->assertNull($country); + $this->assertFalse($country->exists); + } + + public function testLazyLoadingWithDefault() + { + $country = Comment::find(33)->country; + + $this->assertInstanceOf(Country::class, $country); + $this->assertFalse($country->exists); } public function testEagerLoading() @@ -47,7 +56,8 @@ public function testEagerLoading() $this->assertEquals(1, $comments[0]->country->id); $this->assertEquals(2, $comments[1]->country->id); - $this->assertNull($comments[2]->country); + $this->assertInstanceOf(Country::class, $comments[2]->country); + $this->assertFalse($comments[2]->country->exists); } public function testEagerLoadingWithPrefix() @@ -64,7 +74,8 @@ public function testLazyEagerLoading() $this->assertEquals(1, $comments[0]->country->id); $this->assertEquals(2, $comments[1]->country->id); - $this->assertNull($comments[2]->country); + $this->assertInstanceOf(Country::class, $comments[2]->country); + $this->assertFalse($comments[2]->country->exists); } public function testExistenceQuery() diff --git a/tests/Models/Comment.php b/tests/Models/Comment.php index d94872c..cc961dd 100644 --- a/tests/Models/Comment.php +++ b/tests/Models/Comment.php @@ -6,7 +6,7 @@ class Comment extends Model { public function country() { - return $this->belongsToThrough(Country::class, [User::class, Post::class]); + return $this->belongsToThrough(Country::class, [User::class, Post::class])->withDefault(); } public function countryWithCustomForeignKeys()