public static function add($lat, $lng, $name) { $centerObj = new self(); $center = $centerObj->firstOrNew(['requested_lat' => $lat, 'requested_lng' => $lng, 'name' => $name]); if ((int) $center->place_id != 0) { $center->place->bars; return $center; } //add info about center to Places $place = \App\Place::createByCoordinates($lat, $lng); $center->place()->associate($place); $center->save(); //add Bars $resp = \App\Api\Place::findInRange($lat, $lng); $data = $resp->getResult()->results; foreach ($data as $responseData) { $placeItem = \App\Place::create(); \App\Place::bindFromResponse($placeItem, $responseData); $placeItem->save(); $placeItem->centres()->attach($place->id); } return $center; }