parent_id) { $parent = self::find($model->parent_id); $model->server_id = $parent->server_id; $model->outlet_id = date('ymdHi') . mt_rand(100, 999); $model->IdentityUpdate(2); } else { $model->server_id = date('Ymd') . mt_rand(100000, 999999); $model->server_key = Str::random(32); $model->des3key = Str::random(24); $model->IdentityUpdate(1); } $model->info()->create([ 'nickname' => $model->username, ]); $model->save(); }); } /** * 为数组 / JSON 序列化准备日期。 * @param \DateTimeInterface $date * @return string */ protected function serializeDate(DateTimeInterface $date) { return $date->format($this->dateFormat ?: 'Y-m-d H:i:s'); } protected function getNicknameAttribute() { return $this->info ? $this->info->nickname : ''; } public function info() { return $this->hasOne(UserInfo::class); } //渠道商配置 public function code() { return $this->hasMany(UserCode::class); } public function parent() { return $this->hasOne(User::class, 'id', 'parent_id'); } /** * Notes: 密码加密 * @Author: * @Date : 2019/9/6 11:37 * @param $password */ protected function setPasswordAttribute($password) { if (!empty($password)) { $this->attributes['password'] = bcrypt($password); } } public function getTypeTextAttribute() { switch ($this->type) { case 'pingan': return '平安券'; break; case 'wo': return '沃支付'; break; default: return '未知'; break; } } /** * 关联平安卡券核销 * @author 玄尘 2020-04-03 * @return [type] [description] */ public function coupons() { return $this->hasMany(Coupon::class); } public function getCouponCount($type, $date = '') { return $this->coupons() ->whereIn('status', [2]) ->where('thirdPartyGoodsId', $type) ->when($date, function ($q) { $q->whereDate('created_at', now()->format('Y-m-d')); }) ->count(); } }