belongsTo(User::class); } protected static function init() { self::beforeWrite(function (self $address){ /*if(empty($address['city'])) { $address['city'] = tm()->getLocation($address['longitude'], $address['latitude'])['city']; }*/ if(!isset($address['is_default'])){ $address['is_default']=0; } $address['area']=implode(',',Area::getTreeId($address['city'])); $address['full_address']=Area::getNameString($address['area']).$address['address']; }); self::afterWrite(function (self $address){ if($address['is_default']==1){ self::where('user_id',$address['user_id'])->where('id','<>',$address['id'])->update(['is_default'=>0]); } }); self::beforeInsert(function (self $address){ /*if(empty($address['location'])){ $address['location']=tm()->getLocation($address['longitude'],$address['latitude'])['address']; }*/ }); } public function scopeExists(Query $query,$data){ if(!empty($data['name'])){ $query->where('name',$data['name']); } if(!empty($data['mobile'])){ $query->where('mobile',$data['mobile']); } if(!empty($data['address'])){ $query->where('address',$data['address']); } if(!empty($data['city'])){ $query->where('city',$data['city']); } if(!empty($data['longitude'])){ $query->where('longitude',$data['longitude']); } if(!empty($data['latitude'])){ $query->where('latitude',$data['latitude']); } } public function getCityDetailAttr($a,$b){ $city=$b['city']; if(!$city){ return ''; } $area=Area::where('id|name|shortname',$city)->find(); return $area['mergename']??''; } public function orderAddress(){ return [ 'name'=>$this['name'], 'mobile'=>$this['mobile'], 'address'=>$this['full_address'], 'city'=>$this['city'], 'area'=>$this['area'], ]; } }