Friday, March 17, 2017

Laravel and ChartJS - Trying to get count of array

I am trying to create a chart from my MySQL db for use in Laravel.

To test the chart, I put in an array like [1, 2, 3, 4, 5] and it works perfectly. But when I try to grab data from my db, it doesn't work. I have a db with the following fields:

id  
project  
stage  
status  
timestamps

What I am trying to show in my chart are the number of projects in each stage. So the stages are:

Waiting to Start  
In Progress  
Waiting for Information  
Pre-Launch  
Launched

My query is the following:

$total = Project::all()->groupBy('stage')->count('stage');

That does not work. I've tried a couple other queries but not sure how to get the number of projects in each stage and output that to an array. Also, would like to get the data in the particular order which is not alphabetical or anything so cannot use orderBy, etc.

Thank you for your help.



via robk27

Advertisement