0

Sorry for the beginner question.

I'm trying to use PHP array to Javascript variable.

But I got below console error: enter image description here

This is my PHP,

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

?>

And This is my index.blade.php,

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

Could you help point out where did I go wrong?

1 Answer 1

2

When you use {{ }} the data/text echo'd is escaped.

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

You just need to change these brackets to {!! !!} to avoid escaping the information:

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

You can read more about this in the docs.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! successed!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.