0

As you can see my code below, I have defined a $test variable in controller. The problem is, when I passing the variable to js, it became different result.

Controller

$test = "{name: 'John',color: 'olive',data: [5, 3, 4, 7, 2],stack: '2014',xAxis: 1}";

View

<script type="text/javascript">
  var test = {{$test}}; // result is {name: &#039;John&#039;,color: &#039;olive&#039;,data: [5, 3, 4, 7, 2],stack: &#039;2014&#039;,xAxis: 1};
</script>

Inspect

enter image description here

1
  • You should form array (or object) and pass json_encoded($formedObject) instead. Commented Oct 4, 2018 at 10:32

2 Answers 2

5

It's becouse of the nature of blade {{}}. It's removing some characters. Try using {!! $test !!}

https://laravel.com/docs/5.7/blade#displaying-data

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

1 Comment

Update answer with a link
1

In view

var test = {!! $test!!};

Double braces is escaping your characters.

1 Comment

The correct Laravel syntax to not escape is {!! $test !!}

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.