Andrei Nagy
Andrei Nagy

Reputation: 251

How to get data from Laravel db into googlechart?

I want to introduce my counts into this GoogleChart. I want to count all id from table News/Opinions/Events etc.I want to be able to put numbers of all records from News/Opinions/Events etc. If in "News" table I have 10 news, i want to count them and introduce it to ['News', 'All news HERE'],

And the second, I want to add it into this code:

<script>
 google.charts.load("current", {packages:["corechart"]});
      google.charts.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['News', 'All news HERE'],
          ['Events',    No of events from db],


        ]);

        var options = {
          title: 'My Daily Activities',
          pieHole: 0.4,
        };

        var chart = new google.visualization.PieChart(document.getElementById('donutchart'));
        chart.draw(data, options);
      }
</script>

Upvotes: 0

Views: 150

Answers (2)

UnrivaledIr
UnrivaledIr

Reputation: 2106

I really can't fully understand what you are trying to do but I will show you whole concept in web.php route file :

   Route::get('/newsCount', 'NewsController@count');

in NewsController.php controller file :

public function count()
    {
        $CountOfNews = News::all()->count(); // Get count of all your news
        // Now you can use one of these two ways 
        // Pass data to view way 1
        return View::make("user/NewsCount", compact('CountOfNews')); 
        // Pass data to view way 2
        return View::make("user/NewsCount")->with(array('Count'=>$CountOfNews));        
    }

and into your balde.php view file you should do like this :

<script>
 alert({{$CountOfNews}});
</script>

Upvotes: 1

Thepeanut
Thepeanut

Reputation: 3407

You can just query your database to get all counts that you needed (should be fast if you don't query many tables).

public function myControllerAction() {
    $dbData = DB::selectOne('
        select
        (select count(*) from news) as News, 
        (select count(*) from events) as Events, 
        (select count(*) from someothertable) as Someotherdata 
    ');

    return view('my-view', compact('dbData'));
}

After this you can use $dbData->News, $dbData->Events, $dbData->Someotherdata and put it anywhere you like in your code.

You can even simplify your future development by using Laravel's Collection and generating a ready to use array for google charts:


public function myControllerAction() {
    $dbData = DB::selectOne('
        select
        (select count(*) from news) as News, 
        (select count(*) from events) as Events, 
        (select count(*) from someothertable) as Someotherdata 
    ');

    $collection = new \Illuminate\Support\Collection($dbData);
    $data = $collection->flatMap(function($value, $key) {
        // We will use name of the key as the label for chart (News, Events, Someotherdata)
        return [[$key, $value]];
    })->toArray();

    return view('my-view', compact('data'));
}

And just use Blade's @json directive to render the json where you need it:

<script>
 google.charts.load("current", {packages:["corechart"]});
      google.charts.setOnLoadCallback(drawChart);
      function drawChart() {
        // Here is the changed part, notice the @json($data) here
        var data = google.visualization.arrayToDataTable(@json($data));

        var options = {
          title: 'My Daily Activities',
          pieHole: 0.4,
        };

        var chart = new google.visualization.PieChart(document.getElementById('donutchart'));
        chart.draw(data, options);
      }
</script>

Upvotes: 1

Related Questions