Example #1
0
 public static function createByCoordinates($lat, $lng)
 {
     $placeObj = new self();
     $place = $placeObj->firstOrNew(['lat' => $lat, 'lng' => $lng]);
     if ((int) $place->id > 0) {
         return $place;
     }
     /** @var $resp \App\Lib\Api\GooglePlace\Response * */
     $resp = \App\Api\Place::findClosest($lat, $lng);
     $data = $resp->getResult();
     $results = (array) $data->results;
     if (empty($results)) {
         throw new Exception('no results found for lat:' . $lat . ',lng:' . $lng);
     }
     $stdPlace = array_pop($results);
     $place = $placeObj->firstOrNew(['lat' => $stdPlace->geometry->location->lat, 'lng' => $stdPlace->geometry->location->lng]);
     self::bindFromResponse($place, $stdPlace);
     $place->save();
     return $place;
 }