Laravel:解析错误:语法错误,视图上出现意外的“ if”(T_IF)

时间:2018-07-13 10:09:50

标签: php mysql laravel google-maps

我有一个laravel程序代码,它将执行2个数据。如果查询中的数据存在“ banjar”,它将显示从banjar中心点到目标点的距离。否则它将显示从其他中心点到目的地点的距离。

但是执行此代码时出现错误“解析错误:语法错误,意外的'if'(T_IF)”。

视图中的代码

<tbody>
                        @php
                            //TASIK
                            $latitude1 = -7.325374900000001;
                            $longitude1 = 108.2251681;

                            //BANJAR
                            $latitude2 = -7.3708991;
                            $longitude2 = 108.5418788;

                            $latitudersk = $data->latitude;
                            $longitudersk = $data->longitude;

                            @if( {{ $rsk_tasik }} )
                                $dataJson = file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=".$latitude1.",".$longitude1."&destinations=".$latitudersk.",".$longitudersk."&key=AIzaSyCyUBu8OPh0adMWbL9McFMMDqJA9UB7jes");
                            @elseif( {{ $rsk_banjar }} )
                                $dataJson = file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=".$latitude2.",".$longitude2."&destinations=".$latitudersk.",".$longitudersk."&key=AIzaSyCyUBu8OPh0adMWbL9McFMMDqJA9UB7jes");
                            @endif

                            $json = json_decode($dataJson);
                            $distance = $json->rows[0]->elements[0]->distance->text;
                        @endphp
                    @foreach($rsk as $no => $data)
                    <tr>

                        <td>{{ $no +1 }}</td>
                        <td>{{ $data->address }}</td>
                        <td>{{ $data->latitude }}</td>
                        <td>{{ $data->longitude }}</td>
                        <td>{{ $distance }}</td>
                    </tr>
                    @endforeach
                    </tbody>

在Controller中,我使用这样的查询

$rsk = DB::select("SELECT * FROM gmaps_geocache WHERE address LIKE '%RSK%'");
        $rsk_tasik = DB::select("SELECT * FROM gmaps_geocache WHERE address LIKE '%RSK%' AND address NOT LIKE '%banjar%'");
        $rsk_banjar = DB::select("SELECT * FROM gmaps_geocache WHERE address LIKE '%RSK%' AND address LIKE '%banjar%'");

如何解决它和视图是这样的 enter image description here

2 个答案:

答案 0 :(得分:2)

不要在@php和@if中使用{{ }}

<tbody>
                    @php
                        //TASIK
                        $latitude1 = -7.325374900000001;
                        $longitude1 = 108.2251681;

                        //BANJAR
                        $latitude2 = -7.3708991;
                        $longitude2 = 108.5418788;

                        $latitudersk = $data->latitude;
                        $longitudersk = $data->longitude;

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

                        } elseif($rsk_banjar) {
                               $dataJson = file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=".$latitude2.",".$longitude2."&destinations=".$latitudersk.",".$longitudersk."&key=AIzaSyCyUBu8OPh0adMWbL9McFMMDqJA9UB7jes");
                        }

                           $json = json_decode($dataJson);
                           $distance = $json->rows[0]->elements[0]->distance->text;
                        @endphp
                @foreach($rsk as $no => $data)
                <tr>

                    <td>{{ $no +1 }}</td>
                    <td>{{ $data->address }}</td>
                    <td>{{ $data->latitude }}</td>
                    <td>{{ $data->longitude }}</td>
                    <td>{{ $distance }}</td>
                </tr>
                @endforeach
                </tbody>

答案 1 :(得分:2)

您可以尝试以下方法:

@php
//TASIK
$latitude1 = -7.325374900000001;
$longitude1 = 108.2251681;

//BANJAR
$latitude2 = -7.3708991;
$longitude2 = 108.5418788;

$latitudersk = $data->latitude;
$longitudersk = $data->longitude;

if($rsk_tasik)
{
    $dataJson = file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=".$latitude1.",".$longitude1."&destinations=".$latitudersk.",".$longitudersk."&key=AIzaSyCyUBu8OPh0adMWbL9McFMMDqJA9UB7jes");
}
else 
{
    $dataJson = file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=".$latitude2.",".$longitude2."&destinations=".$latitudersk.",".$longitudersk."&key=AIzaSyCyUBu8OPh0adMWbL9McFMMDqJA9UB7jes");
}
$json = json_decode($dataJson);
$distance = $json->rows[0]->elements[0]->distance->text;
@endphp