zilong 4 年 前
コミット
df4949a07b
3 ファイル変更24 行追加3 行削除
  1. 16 0
      app/Helpers/functions.php
  2. 6 1
      app/Models/Patient.php
  3. 2 2
      app/Models/User.php

+ 16 - 0
app/Helpers/functions.php

xqd
@@ -105,3 +105,19 @@ if (!function_exists('build_sn')) {
         return $sn;
     }
 }
+
+//生日转年龄
+if (!function_exists('birthday_to_age')){
+    function birthday_to_age($birthday)
+    {
+        list($year, $month, $day) = explode("-", $birthday);
+        $year_diff = (date("Y") - $year) > 0 ? date("Y") - $year.'岁':'';
+        $month_diff = (date("m") - $month) > 0 ? date("m") - $month.'个月':'';
+        $day_diff = (date("d") - $day) > 0 ? date("d") - $day.'天':'';
+        if ($day_diff < 0 || $month_diff < 0) {
+            $year_diff--;
+        }
+
+        return $year_diff.$month_diff.$day_diff ;
+    }
+}

+ 6 - 1
app/Models/Patient.php

xqd
@@ -10,5 +10,10 @@ namespace App\Models;
 
 class Patient extends BaseModel
 {
-
+    protected $appends = ['is_collect'];
+    
+    public function getAgeAttribute()
+    {
+        return birthday_to_age($this->birthday);
+    }
 }

+ 2 - 2
app/Models/User.php

xqd
@@ -10,9 +10,9 @@ namespace App\Models;
 
 class User extends BaseModel
 {
-    public function getCouponNumAttr($value, $data)
+    public function getCouponNumAttribute()
     {
-        return UserCoupon::where('user_id', $data['id'])->where('status', 1)->where('expire_time', '>', time())->count();
+        return UserCoupon::where('user_id', $this->id)->where('status', 1)->where('expire_time', '>', time())->count();
     }
 
     //通过token获取用户信息