'boolean', ]; /** * 不参与版本记录的字段 * @var array|string[] */ protected array $dontVersionable = ['updated_at']; /** * @var array */ protected $guarded = [ 'created_at', 'updated_at', ]; public static function boot() { parent::boot(); /** * 如果当前地址为默认地址,取消其他地址的默认地址设置 */ self::saved(function ($model) { if ($model->is_default && $model->id) { Address::where('id', '<>', $model->id) ->where('user_id', $model->user_id) ->where('is_default', 1) ->update(['is_default' => 0]); } // else { // Address::where('user_id', $model->user_id) // ->where('is_default', 1) // ->update(['is_default' => 0]); // } }); } /** * Notes: 保存默认地址时转换格式,非0的都转换成0 * @Author: * @Date : 2020/11/5 5:23 下午 * @param $value */ protected function setIsDefaultAttribute($value): void { if (strtolower($value) === 'false' || $value === '0') { $this->attributes['is_default'] = 0; } else { $this->attributes['is_default'] = !!$value; } } /** * Notes : 将地址设置为默认地址 * @Date : 2021/4/23 1:34 下午 * @Author : < Jason.C > */ public function setDefault(): void { $this->is_default = 1; $this->save(); } }