2017-03-21 21 views
0

laravel 5.4がフェッチメソッドを削除したので、ちょうど質問したいと思います... 私のコードに使用できる最良の方法は何ですか? は、ここに私のコードです:Laravel 5.4 jsonのデータベースからフェッチする

@phpクエリを通じて

$someArray = array(); 

//ループと$ someArrayに結果をプッシュします。

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 

私は「タイトル」、「緯度」、「LNG」と私のマップのための変数として、「説明」を必要とするので.. ここに私のマップのコードがあります:私は誰にも役立つことを願って:) )

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つの文字列を使用しようとしている 'LAT' = > $行[ '緯度']、 'LNG' => $行[ '経度']、 '記述' => $行[ 'enrollment_sy_2014_2015']ここで使用される : するvar myLatlng =新しいですgoogle.maps.LatLng(data.lat、data.lng); 位置:myLatlng、 マップ:マップ、 タイトル:data.title そして、ここで: infoWindow.setContent( "

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

+0

私は自分の答えを編集しました。それが今あなたのニーズに合っているかどうかを見てください。 Webページには何もレンダリングしないので – Jachinair

+0

うーん...私は、ソースコードを見たし、これは私が得たものである: VARマーカー= HTTP/1.0 200 OK のCache-Control:キャッシュなし、プライベート のContent-Type:アプリケーション/ json [{"title": "バンゴーNHS"、 "ラト": "7.118366409"、 "lng": "125.653946300"、 "description": "4927"}、{"title": "F" Bustamante NHS "、" lat ":" 7.190484185 "、" lng ":" 125.646700700 "、" description ":" 3299 "}] ; それでも、私はあなたの答えをチェックします。ありがとうございます:)) – jeanawhou

関連する問題