int("customer_id"); $customer = Customer::require($customer_id); } $location->customer_id = $customer->id; $location->name = $request->string("name"); $location->nickname = $request->string("nickname"); $location->phone = $request->string("phone"); $location->email = $request->email("email"); $location->contact_name = $request->string("contact_name"); $location->contact_phone = $request->string("contact_phone"); $location->contact_email = $request->email("contact_email"); $address = $location->getAddress(); $address->address_1 = $request->string("address_1"); $address->address_2 = $request->string("address_2"); $address->city = $request->string("city"); $address->county = $request->string("county"); $address->postal_code = $request->string("postal_code"); $address->country = $request->string("country"); $address->latitude = $request->float("latitude"); $address->longitude = $request->float("longitude"); if(!$location->name) { throw new RuntimeException(__("Name is required.")); } if(isset($address->postal_code, $address->country) && !$address->hasGeolocation()) { $address->geocode(); } $address->commit(); $location->address_id = $address->id; $location->commit(); return $location; } // Object methods /** * @return Customer * @throws Exception */ public function getCustomer(): Customer { return Customer::get($this->customer_id); } /** * @return Address * @throws Exception */ public function getAddress(): Address { $address = Address::get($this->address_id); return $address ?: new Address(); } public function getSlug(): string { return Util::slug("$this->name $this->id"); } /** * @return string * @throws Exception */ public function getUrl(): string { return sprintf("/locations/%s/update", $this->getSlug()); } /** * @return string * @throws Exception */ public function getDisplayName(): string { $customer = $this->getCustomer(); return sprintf("%s - %s", $customer->name, $this->name); } }