0

I create query in laravel 5.6

I need print json array in Jquery.

Return query in laravel is:

[
    {
        "day": 19,
        "count": 1
    },
    {
        "day": 18,
        "count": 3
    }
]

Script code is:

var chart = AmCharts.makeChart( "chartdiv", {
        "type": "serial",
        "theme": "light",
        "dataProvider": [
            {
                "day": 19,
                "count": 1
            },
            {
                "day": 18,
                "count": 3
            }
        ].reverse(),
        .
        .
        .
        .

I place {{ json_encode($query) }} in Jquery code:

var chart = AmCharts.makeChart( "chartdiv", {
    "type": "serial",
    "theme": "light",
    "dataProvider": [ {{ json_encode($query) }} ].reverse(),
    .
    .
    .
    .

But this code is not work...

How to issue this problem?

2
  • 2
    Given that the properties in the array you return are different to those expected in the actual code, I'm not sure why you'd expect this to work. You need to change the day and count property names. Also note that you don't need the extra [] around {{ json_encode($query) }} Commented Apr 19, 2018 at 14:57
  • @RoryMcCrossan Hi, my question is update. I can't remove [] becouse use ].reverse(). Commented Apr 19, 2018 at 15:01

2 Answers 2

2

You need to parse json and remove an addition an array like below:

var chart = AmCharts.makeChart( "chartdiv", {
    "type": "serial",
    "theme": "light",
    "dataProvider": JSON.parse('{{ json_encode($query) }}').reverse(),
})

Hope this will help you

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

3 Comments

Uncaught SyntaxError: Unexpected token & in JSON at position 2 at JSON.parse (<anonymous>)
"dataProvider": JSON.parse('[{&quot;day&quot;:19,&quot;count&quot;:1},{&quot;day&quot;:18,&quot;count&quot;:3}]').reverse(),
You have two way to fix this problem: 1) JSON.parse('{!! json_encode($query) !!}').reverse() 2) JSON.parse('<?php echo json_encode($query); ?>').reverse()
0

Replace dataProvider by the following code

"dataProvider": JSON.parse(<?=json_encode($query);?>).reverse()

Comments

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.