1

I have this controller:

public function main()
{
    $user=User::find(1);
    return view('home')->with('user',$user);
}

And in the home.blade.php view, i need to get the user's attributes travel, food, misc. I can get those using $user->travel, $user->food, $user->misc which are integers. but, i have to show these expenses in pie chart. So, i use chart.js

    Travel Expenses:{{$user->travel}}<br>
    Food Expenses:{{$user->food}}<br>
    Miscellaneous Expenses:{{$user->misc}}<br>

    <canvas id="myChart"></canvas>

    <script>
     var ctx = document.getElementById("myChart");
            var data = {
                labels: [
                    "Travel",
                    "Food",
                    "Miscellaneous"
                ],
                datasets: [
                    {
                        data: [300, 50, 100],
                        backgroundColor: [
                            "#FF6384",
                            "#36A2EB",
                            "#FFCE56"
                        ],
                        hoverBackgroundColor: [
                            "#FF6384",
                            "#36A2EB",
                            "#FFCE56"
                        ]
                    }]
            };
        </script>

This will show static data 300,50, and 100. How can i add these data dynamically? I need data: [300, 50, 100], to be something like data: [$user->travel, $user->food, $user->misc],

2
  • I don't know PHP but all you need to do is render them from your php server inline so i would guess [{{$user->travel}}, {{$user->food}}, {{$user->misc}}] directly in your javscript code, PHP will render the 300,50,100 Commented Apr 17, 2017 at 14:14
  • Cleary I do know PHP! Commented Apr 17, 2017 at 14:15

4 Answers 4

2

You can do something like that in your controller:

public function main()
{
    $user = User::find(1);
    $data = [
        'user'      => $user,
        'dataChart' => [$user->travel, $user->food, $user->misc]
    ];
    return view('home', $data);
}

and in your view:

<script>
var ctx = document.getElementById("myChart");
var data = {
    labels: [
      "Travel",
      "Food",
      "Miscellaneous"
    ],
    datasets: [{
        data: {{$dataChart}},
        backgroundColor: [
            "#FF6384",
            "#36A2EB",
            "#FFCE56"
        ],
        hoverBackgroundColor: [
            "#FF6384",
            "#36A2EB",
            "#FFCE56"
        ]
    }]
};
</script>

That's resolves your problem.

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

Comments

1

Use the same notation:

data: [{{$user->travel}}, {{$user->food}}, {{$user->misc}}]

That way the template engine will render the values and when loaded in browser then javascript will use it.

Comments

1

Try :

<?php

$travel = $user->travel;
$food = $user->food;
$misc =  $user->misc;

$data = "[$travel,$food,$misc]";
?>

Travel Expenses:{{$travel}}<br>
    Food Expenses:{{$food}<br>
    Miscellaneous Expenses:{{$misc}}<br>

    <canvas id="myChart"></canvas>

    <script>
     var ctx = document.getElementById("myChart");
            var data = {
                labels: labels: [
                "Travel",
                "Food",
                "Miscellaneous",
                datasets: [
                    {
                        data: {{$data}},
                        backgroundColor: [
                            "#FF6384",
                            "#36A2EB",
                            "#FFCE56"
                        ],
                        hoverBackgroundColor: [
                            "#FF6384",
                            "#36A2EB",
                            "#FFCE56"
                        ]
                    }]
            };
        </script>

Comments

1
        Travel Expenses:<span id='travel'>{{$user->travel}}</span><br>
 Food Expenses:<span id='food'>{{$user->food}}</span><br>
 Miscellaneous Expenses:<span id='misc'>{{$user->misc}}</span>

        <canvas id="myChart"></canvas> <script> 
        var ctx = document.getElementById("myChart");

        var data = { 
           labels: [
              "Travel", 
              "Food", 
              "Miscellaneous"
           ], 
          datasets: [ { 
             data: [
                document.getElementById('travel').innerHTML, 
    document.getElementById('food').innerHTML, 
    document.getElementById('misc').innerHTML],
             backgroundColor: [ "#FF6384", "#36A2EB", "#FFCE56" ], 
             hoverBackgroundColor: [ "#FF6384", "#36A2EB", "#FFCE56" ] 
           }] 
        }; 
    </script>


Try this.

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.