0

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>

3
  • you have to get the data in the controller and then pass it in to the view. print your data in the desired format, an array of string. The html and js are executed after the page is loaded Commented Jun 25, 2019 at 10:45
  • and how can I get the data in the controller ? Commented Jun 25, 2019 at 10:48
  • by using an sql query or using eloquent model that laravel uses. You should check the documentation Commented Jun 25, 2019 at 10:54

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

Comments

1

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>

2 Comments

I used $CountOfNews = News::all()->count; but I receive all data from database, all columns.I want to receive a number, like 5 or 10 rows.Not all data.
sorry I forget to use (). change $CountOfNews = News::all()->count; to $CountOfNews = News::all()->count(); and you can also try $CountOfNews = News::count();

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.