The catch all for me is taking an object, encoding it, and then passing the string into a javascript script
tag. To do this you have to do some replacements.
First replace every \
with a double slash \\
and then every quote"
with a \"
.
$payload = json_encode($payload);
$payload = preg_replace("_\\\_", "\\\\\\", $payload);
$payload = preg_replace("/\"/", "\\\"", $payload);
return View::make('pages.javascript')
->with('payload', $payload)
Then in the blade template
@if(isset($payload))
<script>
window.__payload = JSON.parse("{!!$payload!!}");
</script>
@endif
This basically allows you to take an object on the php side, and then have an object on the javascript side.