diff --git a/app/User.php b/app/User.php index 17a63c9..c172f5e 100644 --- a/app/User.php +++ b/app/User.php @@ -128,7 +128,7 @@ class User extends Authenticatable public function wifes() { - return $this->belongsToMany(User::class, 'couples', 'husband_id', 'wife_id')->using('App\CouplePivot')->withPivot(['id'])->withTimestamps(); + return $this->belongsToMany(User::class, 'couples', 'husband_id', 'wife_id')->using('App\CouplePivot')->withPivot(['id'])->withTimestamps()->orderBy('marriage_date'); } public function addWife(User $wife, $marriageDate = null) @@ -146,7 +146,7 @@ class User extends Authenticatable public function husbands() { - return $this->belongsToMany(User::class, 'couples', 'wife_id', 'husband_id')->using('App\CouplePivot')->withPivot(['id'])->withTimestamps(); + return $this->belongsToMany(User::class, 'couples', 'wife_id', 'husband_id')->using('App\CouplePivot')->withPivot(['id'])->withTimestamps()->orderBy('marriage_date'); } public function addHusband(User $husband, $marriageDate = null) @@ -170,10 +170,10 @@ class User extends Authenticatable public function couples() { if ($this->gender_id == 1) { - return $this->belongsToMany(User::class, 'couples', 'husband_id', 'wife_id')->using('App\CouplePivot')->withPivot(['id'])->withTimestamps(); + return $this->belongsToMany(User::class, 'couples', 'husband_id', 'wife_id')->using('App\CouplePivot')->withPivot(['id'])->withTimestamps()->orderBy('marriage_date'); } - return $this->belongsToMany(User::class, 'couples', 'wife_id', 'husband_id')->using('App\CouplePivot')->withPivot(['id'])->withTimestamps(); + return $this->belongsToMany(User::class, 'couples', 'wife_id', 'husband_id')->using('App\CouplePivot')->withPivot(['id'])->withTimestamps()->orderBy('marriage_date'); } public function marriages() diff --git a/tests/Unit/UserTest.php b/tests/Unit/UserTest.php index 0908055..3966602 100644 --- a/tests/Unit/UserTest.php +++ b/tests/Unit/UserTest.php @@ -55,9 +55,16 @@ class UserTest extends TestCase $husband->addWife($wife1, '1990-02-13'); $husband->addWife($wife2, '1999-04-21'); - $marriages = $husband->fresh()->marriages; + $husband = $husband->fresh(); + $marriages = $husband->marriages; $this->assertEquals('1990-02-13', $marriages->first()->marriage_date); $this->assertEquals('1999-04-21', $marriages->last()->marriage_date); + + $this->assertEquals($wife1->name, $husband->couples->first()->name); + $this->assertEquals($wife2->name, $husband->couples->last()->name); + + $this->assertEquals($wife1->name, $husband->wifes->first()->name); + $this->assertEquals($wife2->name, $husband->wifes->last()->name); } /** @test */ @@ -69,9 +76,16 @@ class UserTest extends TestCase $wife->addHusband($husband1, '1980-02-13'); $wife->addHusband($husband2, '1989-04-21'); - $marriages = $wife->fresh()->marriages; + $wife = $wife->fresh(); + $marriages = $wife->marriages; $this->assertEquals('1980-02-13', $marriages->first()->marriage_date); $this->assertEquals('1989-04-21', $marriages->last()->marriage_date); + + $this->assertEquals($husband1->name, $wife->couples->first()->name); + $this->assertEquals($husband2->name, $wife->couples->last()->name); + + $this->assertEquals($husband1->name, $wife->husbands->first()->name); + $this->assertEquals($husband2->name, $wife->husbands->last()->name); } /** @test */