December 2017
Beginner to intermediate
376 pages
7h 53m
English
We'll now encode the entire model as JSON within the view. The JSON format is good because it can be stored as a string and can be parsed by both PHP and JavaScript.
In our inline script, let's format the model as a JSON string and assign to a model variable.
resources/views/app.blade.php:
<script type="text/javascript"> var model = "{!! addslashes(json_encode($model)) !!}"; console.log(model); </script>
Notice we also had to wrap json_encode in another global function, addslashes. This function will add backslashes before any character that needs to be escaped. It's necessary to do this because the JavaScript JSON parser doesn't know which quotes in the string are part of the JavaScript syntax, and which are part of the JSON object. ...
Read now
Unlock full access