Put data from database to chart

Published 5 months ago by adnan483

I want to take from database all dates until today or last 15 days and sum of solds for each day and all that put in chart

Route::get('/admin/statistic',function(){
    $montly = DB::table('salles')
    ->select(DB::raw('SUM(total) as total'))
    ->groupBy(DB::raw('DAY(created_at) DESC'));
    return view('admin.statistika.index',compact('montly'));
});

I'm trying to put this inside of this chart can't echo anything from $montly

<script>
  Chart.defaults.global.defaultFontFamily = '-apple-system,system-ui,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif';
Chart.defaults.global.defaultFontColor = '#292b2c';
  var ctx = document.getElementById("poslovanje");
var myLineChart = new Chart(ctx, {
  type: 'line',
  data: {
    labels: ["Mar 1", "Mar 2", "Mar 3", "Mar 4", "Mar 5", "Mar 6", "Mar 7", "Mar 8", "Mar 9", "Mar 10", "Mar 11", "Mar 12", "Mar 13"],
    datasets: [{
      label: "Sessions",
      lineTension: 0.3,
      backgroundColor: "rgba(2,117,216,0.2)",
      borderColor: "rgba(2,117,216,1)",
      pointRadius: 5,
      pointBackgroundColor: "rgba(2,117,216,1)",
      pointBorderColor: "rgba(255,255,255,0.8)",
      pointHoverRadius: 5,
      pointHoverBackgroundColor: "rgba(2,117,216,1)",
      pointHitRadius: 20,
      pointBorderWidth: 2,
      data: [10000, 30162, 26263, 18394, 18287, 28682, 31274, 33259, 25849, 24159, 32651, 31984, 38451],
    }],
  },
  options: {
    scales: {
      xAxes: [{
        time: {
          unit: 'date'
        },
        gridLines: {
          display: false
        },
        ticks: {
          maxTicksLimit: 7
        }
      }],
      yAxes: [{
        ticks: {
          min: 0,
          max: 40000,
          maxTicksLimit: 5
        },
        gridLines: {
          color: "rgba(0, 0, 0, .125)",
        }
      }],
    },
    legend: {
      display: false
    }
  }
});
</script>

BezhanSalleh

if you can't echo anything from monthly it means its null, do a dd($montly) and check if you have data in your db, plus i don't see anything regarding monthly in your chart logic... you might wanna look into this https://laracasts.com/series/charting-and-you

adnan483

@BezhanSalleh thank you for course, I'll check it. I found solution to fill chart but have one more problem, here is my query:

DB::select('SELECT sum(ukupni_dug) AS "ukupno",DAY(created_at) as dan FROM duznicis GROUP BY DAY(created_at) LIMIT 31;');

is possible to add after table name something like WHERE month = current_month because if I add value to 1. January and 1. February, it sum both values, I want to get report for specific month or last 31 day from today?

BezhanSalleh

@adnan483 yes you can, just pass the month, use eloquent or querybuilder it would make it much simpler:

$month = $request->input('month');
DB::table('duznicis')
    ->select('Day(created_at) as dan',DB::raw('sum(ukupni_dug) as ukupno')
        ->whereMonth('created_at',$month)
        ->groupBy('dan')
        ->get();

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