/** * Overwrite Address2 Setter * to geocode the locatable model * */ function setAddress2Attribute($address2) { $this->attributes['address2'] = $address2; $parts = explode(" ", $address2); if (count($parts) > 0 && is_numeric($parts[0])) { $zipcode = Zipcode::where('zipcode', '=', $parts[0])->first(); if ($zipcode instanceof Zipcode) { // connect to Zipcode $this->zipcode_id = $zipcode->id; // Try to geocode if (!$this->geocode()) { $geocoded = $this->geocodeKey(); // use district lat lng if available if ($zipcode->district) { $this->lat = $zipcode->district->lat; $this->lng = $zipcode->district->lng; $this->geosource = 'ZIPD'; $this->geocoded = $geocoded; } else { if ($zipcode->city) { $this->lat = $zipcode->city->lat; $this->lng = $zipcode->city->lng; $this->geosource = 'ZIPC'; $this->geocoded = $geocoded; } } } } } }