Skip to content

Commit

Permalink
Support default models
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir committed Sep 22, 2019
1 parent aea93bd commit cbd9a59
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/Relations/BelongsToThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -208,7 +211,7 @@ protected function buildDictionary(Collection $results)
*/
public function getResults()
{
return $this->first();
return $this->first() ?: $this->getDefaultFor($this->parent);
}

/**
Expand Down
17 changes: 14 additions & 3 deletions tests/BelongsToThroughTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests;

use Tests\Models\Comment;
use Tests\Models\Country;

class BelongsToThroughTest extends TestCase
{
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tests/Models/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit cbd9a59

Please sign in to comment.