From fbdad8a371c70a15a1180eaabeb42baab6d12b6f Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Tue, 4 Dec 2018 20:47:30 +0800 Subject: [PATCH] Order user couples by marriage_date Order female husbands by marriage_date Order male wifes by marriage_date --- app/User.php | 8 ++++---- tests/Unit/UserTest.php | 18 ++++++++++++++++-- 2 files changed, 20 insertions(+), 6 deletions(-) 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 */