getGuarded(); if (count($guarded) === 1 && $guarded[0] === '*') { $model->addFillable([ 'country', 'country_id', 'country_code', 'state', 'state_id', 'state_code' ]); } $model->belongsTo['country'] = ['RainLab\Location\Models\Country']; $model->belongsTo['state'] = ['RainLab\Location\Models\State']; } public function getCountryOptions() { return Country::getNameList(); } public function getStateOptions() { return State::getNameList($this->model->country_id); } /** * Sets the "country" relation with the code specified, model lookup used. * @param string $code */ public function setCountryCodeAttribute($code) { if (!$country = Country::whereCode($code)->first()) { return; } $this->model->country = $country; } /** * Sets the "state" relation with the code specified, model lookup used. * @param string $code */ public function setStateCodeAttribute($code) { if (!$state = State::whereCode($code)->first()) { return; } $this->model->state = $state; } /** * Mutator for "country_code" attribute. * @return string */ public function getCountryCodeAttribute() { return $this->model->country ? $this->model->country->code : null; } /** * Mutator for "state_code" attribute. * @return string */ public function getStateCodeAttribute() { return $this->model->state ? $this->model->state->code : null; } /** * Ensure an integer value is set, otherwise nullable. */ public function setCountryIdAttribute($value) { $this->model->attributes['country_id'] = $value ?: null; } /** * Ensure an integer value is set, otherwise nullable. */ public function setStateIdAttribute($value) { $this->model->attributes['state_id'] = $value ?: null; } }