Browse Source

Order user couples by marriage_date

Order female husbands by marriage_date
Order male wifes by marriage_date
pull/21/head
Nafies Luthfi 7 years ago
parent
commit
fbdad8a371
  1. 8
      app/User.php
  2. 18
      tests/Unit/UserTest.php

8
app/User.php

@ -128,7 +128,7 @@ class User extends Authenticatable
public function wifes() 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) public function addWife(User $wife, $marriageDate = null)
@ -146,7 +146,7 @@ class User extends Authenticatable
public function husbands() 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) public function addHusband(User $husband, $marriageDate = null)
@ -170,10 +170,10 @@ class User extends Authenticatable
public function couples() public function couples()
{ {
if ($this->gender_id == 1) { 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() public function marriages()

18
tests/Unit/UserTest.php

@ -55,9 +55,16 @@ class UserTest extends TestCase
$husband->addWife($wife1, '1990-02-13'); $husband->addWife($wife1, '1990-02-13');
$husband->addWife($wife2, '1999-04-21'); $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('1990-02-13', $marriages->first()->marriage_date);
$this->assertEquals('1999-04-21', $marriages->last()->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 */ /** @test */
@ -69,9 +76,16 @@ class UserTest extends TestCase
$wife->addHusband($husband1, '1980-02-13'); $wife->addHusband($husband1, '1980-02-13');
$wife->addHusband($husband2, '1989-04-21'); $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('1980-02-13', $marriages->first()->marriage_date);
$this->assertEquals('1989-04-21', $marriages->last()->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 */ /** @test */

Loading…
Cancel
Save