最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Passing JSON to frontend using Laravel and Blade - Stack Overflow

programmeradmin1浏览0评论

I am following the Laravel 12 documentation for rendering JSON but the provided example is not working. If I replace {{ Js::from($map) }} with the copy-and-pasted output of console.log(hexes), it works perfectly. So the correct information is being passed from the backend, but it is not encoded(?) in the way Blade(?) expects it to be.

<script>
const hexes = {{ Js::from($map) }};
console.log(hexes);
var hex = new OI.hexmap(document.getElementById('hexmap'),
{
    'grid': {'show':true},
    'label': {
        'show': true, 
        'clip': true, 
        'format': function(txt,attr) {
            return attr.hex.q + ", " + attr.hex.r;
        }
    },
    'hexjson':{
        "layout":"odd-r",
        "hexes": hexes,
    }
}
);
</script>

I am trying to build a HexJSON object for use by Open Innovations' Hexmap Library. When I run the code in the example the library throws errors like Unexpected value translate(NaN NaN) parsing transform attribute. and Unexpected value NaN NaN NaN NaN parsing viewBox attribute. However, when I hardcode the HexJSON, the library works as advertised.

This is the PHP backend. Adding a value with the key "origin" forces json_encode to treat the array as a dictionary, as expected by HexJSON.

$hexes = Hexagon::orderBy('id', 'asc')->get()->toArray();
$hexes["origin"] = array("q"=>0, "r"=>0);
return view('game', ["map" => json_encode($hexes)]);
发布评论

评论列表(0)

  1. 暂无评论