2017-03-21 4 views
0

laravel 5.4가 가져 오기 방법을 제거한 이후로 묻고 싶습니다 ... 내 코드에 사용할 수있는 최선의 방법은 무엇입니까? 다음은 내 코드입니다 :Laravel 5.4 json 용 데이터베이스에서 가져 오기

@php 쿼리를 통해

$someArray = array(); 

// 루프와 $ someArray에 결과를 밀어; :) 그리고 나는 누군가가 도움이 될 수 있습니다 희망 : 내가 '제목', '위도', 'LNG'내지도를위한 변수로 '설명'을 필요로하기 때문에

while ($row = $query->fetch_assoc()) { 
     array_push($someArray, array(
     'title' => $row['school_name'], 
     'lat' => $row['latitude'], 
     'lng' => $row['longitude'], 
     'description' => $row['enrollment_sy_2014_2015'] 
     )); 

    } 

    // Convert the Array to a JSON String and echo it 
    $someJSON = json_encode($someArray); 
    echo $someJSON; 

    @endphp 

에 .. 여기 내지도에 대한 코드입니다)

function LoadMap() { 
    var mapOptions = { 
     center: new google.maps.LatLng(markers[0].lat, markers[0].lng), 
     zoom: 10, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions); 

    //Create and open InfoWindow. 
    var infoWindow = new google.maps.InfoWindow(); 

    for (var i = 0; i < markers.length; i++) { 
     var data = markers[i]; 
     var myLatlng = new google.maps.LatLng(data.lat, data.lng); 
     var marker = new google.maps.Marker({ 
      position: myLatlng, 
      map: map, 
      title: data.title 
     }); 

     //Attach click event to the marker. 
     (function (marker, data) { 
      google.maps.event.addListener(marker, "click", function (e) { 
       //Wrap the content inside an HTML DIV in order to set height and width of InfoWindow. 
       infoWindow.setContent("<div style = 'width:200px;min-height:40px'>" + data.description + "</div>"); 
       infoWindow.open(map, marker); 
      }); 
     })(marker, data); 
    } 
} 

답변

0

Eloquent를 사용하는 경우 Laravel은 컬렉션 및 모델에 toJson() 메서드를 제공합니다.

https://laravel.com/docs/5.4/eloquent-serialization#serializing-to-json

당신은 또한 JSON() 메서드를 사용하여 응답을 변환 할 수 있습니다. 귀하의 경우에는

, 그것은 할 수있는 일 같은 :

foreach($collection as $item) { 
     $data[] = [ 
      'title' => $item->school_name, 
      'lat' => $item->latitude, 
      'lng' => $item->longitude, 
      'description' => $item->enrollment_sy_2014_2015 
     ]; 
    } 
    return response()->json($data); 
+0

하지만 메신저 변수 '제목'=> $ 행 [ 'school_name', '위도'등이 4 개의 문자열을 사용하려고 시도 = > $ 행 [ '찾기'] 'LNG'=> $ 행 [ '경도', '설명'=> $ 행 [ 'enrollment_sy_2014_2015'여기서 사용될 : VAR의 myLatlng = 새로운 google.maps.LatLng (data.lat, data.lng); 위치 : myLatlng, 지도 :지도, 제목 : 여기 data.title 그리고 : infoWindow.setContent ("

" + data.description + "
"); – jeanawhou

+0

내 대답을 편집했습니다. 그것이 당신의 필요를 지금 잘 적응시키는 지보십시오. – Jachinair

+0

흠 ... 웹 페이지가 아무것도 렌더링하지 않기 때문에 나는 소스 코드를 볼 수 있으며 이것은 내가 무엇을 가지고 있습니다 : var에 마커 = HTTP/1.0 200 OK 캐시 제어 : 노 캐시, 개인 콘텐츠 형식 : 응용 프로그램/json [{ "제목": "방공 NHS", "lat": "7.118366409", "lng": "125.653946300", "설명": "4927"}, { "제목": "F. Bustamante NHS ","lat ":"7.190484185 ","lng ":"125.646700700 ","description ":"3299 "}] ; 그럼에도 불구하고, 나는 당신의 대답을 확인할 것입니다 .. 정말 고마워요 :)) – jeanawhou

관련 문제