Laravel:使用数据库字段更改变量的内容

时间:2018-07-12 01:45:20

标签: php mysql laravel

我有代码Laravel 在代码中,我有变量$ latitude1和经度1,并且此变量具有静态内容,如“ 108.2251681”。

$maps = Gmaps_Geocache::all();

        $latitude1 = -7.325374900000001;    
        $longitude1 = 108.2251681;

        $latitude2 = -7.331842799999999;        
        $longitude2 = 108.2237382;

        $dataJson = file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=".$latitude1.",".$longitude1."&destinations=".$latitude2.",".$longitude2."&key=AIzaSyCyUBu8OPh0adMWbL9McFMMDqJA9UB7jes");

如何更改变量的内容“ 108.2251681”并立即从字段数据库中获取?

1 个答案:

答案 0 :(得分:0)

我正在解决它,我把它放在了视图刀片中

<table border="1">
            <tr>
                <th>Address</th>
                <th>Latitude</th>
                <th>Longitude</th>
                <th>Jarak</th>
            </tr>
            @foreach($maps as $m)
            <tr>
                    @php
                        $latitude1 = -7.325374900000001;;   
                        $longitude1 = 108.2251681;

                        $latitude2 = $m->latitude;      
                        $longitude2 = $m->longitude;

                        $dataJson = file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=".$latitude1.",".$longitude1."&destinations=".$latitude2.",".$longitude2."&key=AIzaSyCyUBu8OPh0adMWbL9McFMMDqJA9UB7jes");

                        $json = json_decode($dataJson);
                        $nilaiJarak = $json->rows[0]->elements[0]->distance->text;
                    @endphp
                <td>{{ $m->address }}</td>
                <td>{{ $m->latitude }}</td>
                <td>{{ $m->longitude }}</td>
                <td>{{ $nilaiJarak }}</td>
            </tr>
            @endforeach
        </table>