Browse Source

Added father and mother link method to user model

pull/3/head
Nafies Luthfi 8 years ago
parent
commit
72973cb4f8
  1. 10
      app/User.php
  2. 18
      resources/views/users/partials/parent-spouse.blade.php
  3. 18
      tests/Unit/UserTest.php

10
app/User.php

@ -96,6 +96,16 @@ class User extends Authenticatable
return link_to_route('users.'.$type, $this->name, [$this->id]);
}
public function fatherLink()
{
return $this->father_id ? link_to_route('users.show', $this->father->name, [$this->father_id]) : null;
}
public function motherLink()
{
return $this->mother_id ? link_to_route('users.show', $this->mother->name, [$this->mother_id]) : null;
}
public function wifes()
{
return $this->belongsToMany(User::class, 'couples', 'husband_id', 'wife_id')->withPivot(['id'])->withTimestamps();

18
resources/views/users/partials/parent-spouse.blade.php

@ -19,17 +19,13 @@
</div>
{{ Form::close() }}
@else
@if ($user->father_id)
{{ $user->father->profileLink() }}
@endif
{{ $user->fatherLink() }}
<div class="pull-right">
{{ link_to_route('users.show', 'Set Ayah', [$user->id, 'action' => 'set_father'], ['class' => 'btn btn-link btn-xs']) }}
</div>
@endif
@else
@if ($user->father_id)
{{ $user->father->profileLink() }}
@endif
{{ $user->fatherLink() }}
@endcan
</td>
</tr>
@ -39,7 +35,7 @@
@can ('edit', $currentUser)
@if (request('action') == 'set_mother')
{{ Form::open(['route' => ['family-actions.set-mother', $user->id]]) }}
{!! FormField::select('set_mother_id', $femalePersonList, ['label' => false, 'value' => $user->mother_id, 'placeholder' => 'Pilih dari Laki-laki Terdaftar']) !!}
{!! FormField::select('set_mother_id', $femalePersonList, ['label' => false, 'value' => $user->mother_id, 'placeholder' => 'Pilih dari Wanita Terdaftar']) !!}
<div class="input-group">
{{ Form::text('set_mother', null, ['class' => 'form-control input-sm', 'placeholder' => 'Input Nama Baru...']) }}
<span class="input-group-btn">
@ -49,17 +45,13 @@
</div>
{{ Form::close() }}
@else
@if ($user->mother_id)
{{ $user->mother->profileLink() }}
@endif
{{ $user->motherLink() }}
<div class="pull-right">
{{ link_to_route('users.show', 'Set Ibu', [$user->id, 'action' => 'set_mother'], ['class' => 'btn btn-link btn-xs']) }}
</div>
@endif
@else
@if ($user->mother_id)
{{ $user->mother->profileLink() }}
@endif
{{ $user->motherLink() }}
@endcan
</td>
</tr>

18
tests/Unit/UserTest.php

@ -28,4 +28,22 @@ class UserTest extends TestCase
$this->assertCount(1, $wife->husbands);
$this->assertCount(1, $husband->marriages);
}
/** @test */
public function user_have_father_link_method()
{
$father = factory(User::class)->create();
$user = factory(User::class)->create(['father_id' => $father->id]);
$this->assertEquals($father->profileLink(), $user->fatherLink());
}
/** @test */
public function user_have_mother_link_method()
{
$mother = factory(User::class)->create();
$user = factory(User::class)->create(['mother_id' => $mother->id]);
$this->assertEquals($mother->profileLink(), $user->motherLink());
}
}
Loading…
Cancel
Save