diff --git a/app/Http/Requests/Users/UpdateRequest.php b/app/Http/Requests/Users/UpdateRequest.php index b77795c..289f09e 100644 --- a/app/Http/Requests/Users/UpdateRequest.php +++ b/app/Http/Requests/Users/UpdateRequest.php @@ -30,6 +30,7 @@ class UpdateRequest extends FormRequest 'name' => 'required|string|max:255', 'gender_id' => 'required|numeric', 'dob' => 'nullable|date|date_format:Y-m-d', + 'yob' => 'nullable|date_format:Y', 'dod' => 'nullable|date|date_format:Y-m-d', 'yod' => 'nullable|date_format:Y', 'phone' => 'nullable|string|max:255', @@ -59,6 +60,12 @@ class UpdateRequest extends FormRequest $formData['yod'] = $formData['yod']; } + if ($formData['dob']) { + $formData['yob'] = substr($formData['dob'], 0, 4); + } else { + $formData['yob'] = $formData['yob']; + } + if ($formData['password']) { $formData['password'] = bcrypt($formData['password']); } diff --git a/app/User.php b/app/User.php index 11257d7..cd98314 100644 --- a/app/User.php +++ b/app/User.php @@ -2,6 +2,7 @@ namespace App; +use Carbon\Carbon; use Ramsey\Uuid\Uuid; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; @@ -27,7 +28,7 @@ class User extends Authenticatable 'nickname', 'gender_id', 'name', 'email', 'password', 'address', 'phone', - 'dob', 'dod', 'yod', 'city', + 'dob', 'yob', 'dod', 'yod', 'city', 'father_id', 'mother_id', 'parent_id', ]; @@ -230,4 +231,57 @@ class User extends Authenticatable { return $this->hasMany(Couple::class, 'manager_id'); } + + public function getAgeAttribute() + { + $ageDetail = null; + $yearOnlySuffix = Carbon::now()->format('-m-d'); + + if ($this->dob && !$this->dod) { + $ageDetail = Carbon::parse($this->dob)->diffInYears(); + } + if (!$this->dob && $this->yob) { + $ageDetail = Carbon::parse($this->yob.$yearOnlySuffix)->diffInYears(); + } + if ($this->dob && $this->dod) { + $ageDetail = Carbon::parse($this->dob)->diffInYears($this->dod); + } + if (!$this->dob && $this->yob && !$this->dod && $this->yod) { + $ageDetail = Carbon::parse($this->yob.$yearOnlySuffix)->diffInYears($this->yod.$yearOnlySuffix); + } + if ($this->dob && $this->yob && $this->dod && $this->yod) { + $ageDetail = Carbon::parse($this->dob)->diffInYears($this->dod); + } + + return $ageDetail; + } + + public function getAgeDetailAttribute() + { + $ageDetail = null; + $yearOnlySuffix = Carbon::now()->format('-m-d'); + + if ($this->dob && !$this->dod) { + $ageDetail = Carbon::parse($this->dob)->timespan(); + } + if (!$this->dob && $this->yob) { + $ageDetail = Carbon::parse($this->yob.$yearOnlySuffix)->timespan(); + } + if ($this->dob && $this->dod) { + $ageDetail = Carbon::parse($this->dob)->timespan($this->dod); + } + if (!$this->dob && $this->yob && !$this->dod && $this->yod) { + $ageDetail = Carbon::parse($this->yob.$yearOnlySuffix)->timespan($this->yod.$yearOnlySuffix); + } + if ($this->dob && $this->yob && $this->dod && $this->yod) { + $ageDetail = Carbon::parse($this->dob)->timespan($this->dod); + } + + return $ageDetail; + } + + public function getAgeStringAttribute() + { + return '