Adding values into an array.

Posted 7 months ago by joshblevins

I am trying to build data for a chart. I have a working query which pulls the data from my db. However there is times where their is a month that does not have any data.

The chart displays the last 12 months of medication usage per medication.....

I was thinking that I need to do a for each loop to build an array of data. However wasn't sure exactly how to go about that. If someone could lead me into the right direction or tutorial that would be appreciated.


        $nw = NarcoticWaste::with(['vial' => function($query) 
        $query->select('medication', DB::Raw('MONTH(created_at) month'),DB::raw('count(*) as use_count'), DB::Raw('MONTH(date) monnum'));
        $query->groupBy('medication', 'month');
    }, 'vial.medications'])
        ->orderBy('monnum', 'desc')
    foreach($nw as $n)


var ctxL = document.getElementById("lineChart").getContext('2d');
var myLineChart = new Chart(ctxL, {
  type: 'line',
  data: {
    labels: [<?php echo $labels ?>],
    datasets: [{
        label: "Insufficient",
        data: [{{$nw->where('grade', 2)->pluck('count')->implode(', ')}}],
        backgroundColor: [
          'rgba(105, 0, 132, .2)',
        borderColor: [
          'rgba(200, 99, 132, .7)',
        borderWidth: 2
        label: "Sufficient",
        data: [{{$nw->where('grade', 1)->pluck('count')->implode(', ')}}],
        backgroundColor: [
          'rgba(0, 137, 132, .2)',
        borderColor: [
          'rgba(0, 10, 130, .7)',
        borderWidth: 2
  options: {
    responsive: true

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.