【问题标题】:Laravel 5.4 fetch from database for jsonLaravel 5.4 从数据库中获取 json
【发布时间】:2017-08-13 01:28:06
【问题描述】:

我只是想问一下,因为 laravel 5.4 删除了 fetch 方法...... 什么是我可以用于我的代码的最佳选择。 这是我的代码:

@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

因为我需要 'title'、'lat'、'lng' 和 'description' 作为我地图的变量.. 这是我的地图的代码:我希望任何人都可以提供帮助:))

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);
    }
}

【问题讨论】:

    标签: javascript arrays json laravel-5.4 laravel-eloquent


    【解决方案1】:

    如果你使用 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);
    

    【讨论】:

    • 但我试图将这 4 个字符串用作变量 'title' => $row['school_name'], 'lat' => $row['latitude'], 'lng' => $ row['longitude'], 'description' => $row['enrollment_sy_2014_2015'] 将在这里使用: var myLatlng = new google.maps.LatLng(data.lat, data.lng);位置:myLatlng,地图:地图,标题:data.title 这里:infoWindow.setContent("
      " + data.description + "
      ") ;
    • 我编辑了我的答案。看看它现在是否更适合您的需要。
    • 嗯...我查看了源代码,因为网页没有呈现任何内容,这就是我得到的:var markers = HTTP/1.0 200 OK Cache-Control: no-cache, private Content-Type: application/json [{"title":"F. Bangoy NHS","lat":"7.118366409","lng":"125.653946300","description":"4927"},{"title":"F. Bustamante NHS","lat":"7.190484185","lng":"125.646700700","description":"3299"}] ;不过,我会检查你的答案.. 非常感谢:))
    • 我忘了我可以使用 echo json_encode($data);我的地图现在正在工作。再次感谢@Jachinair
    • 我不知道你的代码,但如果你使用 Laravel,我认为最好返回一个 HTTP 响应来遵循你的应用程序的工作流程,而不是在两个函数之间回显某些东西。但是很难知道为什么 return response()-&gt;json($data); 在您的情况下没有呈现任何内容。
    猜你喜欢
    • 2017-07-08
    • 2014-10-10
    • 1970-01-01
    • 2018-03-04
    • 1970-01-01
    • 2019-03-04
    • 2020-03-19
    • 2017-11-29
    • 1970-01-01
    相关资源
    最近更新 更多