2016-08-30 2 views
0

초보자 용의 질문은 죄송합니다.Laravel 5.2 PHP 배열 - 자바 스크립트 변수

PHP 배열을 Javascript 변수로 사용하려고합니다. , 이건 내 PHP입니다 enter image description here

<?php 
namespace App\Http\Controllers\test; 

use Illuminate\Http\Request; 

use App\Http\Requests; 
use App\Http\Controllers\Controller; 

use DB; 

class PricePinpointController extends Controller 
{ 
    function index(Request $request){ 

    $test = DB::connection('test')->table('aaa')->where('seq', '111')->select('longitude', 'latitude')->get(); 

    $position = array($test[0]->longitude, $test[0]->latitude); 

    return view('index', ['center' => json_encode($position)]); 
    } 
} 

?> 

을 그리고 이것은

<script> 
$(document).ready(function(){ 
    console.log({{$center}}); 
}); 
</script> 

당신은 점을 도울 수있는, 내 index.blade.php입니다 :

하지만 콘솔 오류가 아래에있어 어디서 잘못 됐어?

답변

2

{{ }}을 사용하면 데이터/텍스트 에코가 이스케이프됩니다.

By default, Blade {{ }} statements are automatically sent through PHP's htmlentities function to prevent XSS attacks.

당신은 단지 정보를 탈출 방지하기 위해 {!! !!} 이러한 브래킷을 변경해야

<script> 
$(document).ready(function(){ 
    console.log({!! $center !!}); 
}); 
</script> 

당신은 docs이 대한 자세한 내용을보실 수 있습니다.

+0

대단히 감사합니다! 성공! – jonggu

+0

@jonggu. 큰! 당신의 문제를 해결할 수 있다면 이것을 허용 된 것으로 표시해 주시겠습니까? – James