From 9c5c3838a3b21c4b911e69b1ad60bac7bf4718b1 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Sat, 8 Jun 2019 09:51:13 +0800 Subject: [PATCH] Add age_string attribute on User model Apply user->age_string to user profile views --- app/User.php | 5 +++++ resources/views/users/partials/profile.blade.php | 2 +- resources/views/users/search.blade.php | 2 +- tests/Unit/UserTest.php | 13 +++++++++++++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/app/User.php b/app/User.php index aa24dcf..cd98314 100644 --- a/app/User.php +++ b/app/User.php @@ -279,4 +279,9 @@ class User extends Authenticatable return $ageDetail; } + + public function getAgeStringAttribute() + { + return '
'.$this->age.' '.trans_choice('user.age_years', $this->age).'
'; + } } diff --git a/resources/views/users/partials/profile.blade.php b/resources/views/users/partials/profile.blade.php index 3438ab9..56e4d8a 100644 --- a/resources/views/users/partials/profile.blade.php +++ b/resources/views/users/partials/profile.blade.php @@ -33,7 +33,7 @@ @endif {{ trans('user.age') }} -
{{ $user->age }} {{ trans_choice('user.age_years', $user->age) }}
+ {!! $user->age_string !!} @if ($user->email) diff --git a/resources/views/users/search.blade.php b/resources/views/users/search.blade.php index be01f27..ffad122 100644 --- a/resources/views/users/search.blade.php +++ b/resources/views/users/search.blade.php @@ -30,7 +30,7 @@
{{ userPhoto($user, ['style' => 'width:100%;max-width:300px']) }} @if ($user->age) -
{{ $user->age }} {{ trans_choice('user.age_years', $user->age) }}
+ {!! $user->age_string !!} @endif
diff --git a/tests/Unit/UserTest.php b/tests/Unit/UserTest.php index b2ce620..ba231d5 100644 --- a/tests/Unit/UserTest.php +++ b/tests/Unit/UserTest.php @@ -212,4 +212,17 @@ class UserTest extends TestCase ['2018-02-02', null, '1997', null, '2017', '20 tahun'], ]; } + + /** @test */ + public function user_has_age_string_attribute() + { + $today = '2018-02-02'; + Carbon::setTestNow($today); + $user = factory(User::class)->make(['dob' => '1997-01-01']); + + $ageString = '
21 tahun
'; + $this->assertEquals($ageString, $user->age_string); + + Carbon::setTestNow(); + } }