Home > Net >  How to display data by current month and display no data message if data not exists
How to display data by current month and display no data message if data not exists

Time:05-10

I want to show data by current month and show 'no data to display' message if no data exists on current month. I managed to show all data ascending by date, but this time I only want it to display the data by current month. Oh, I'm using Laravel & ChartJS.

Here is the data query on controller :

$achieveDaily = DailyProduction::orderBy('tgl', 'asc')->get();

Here is the foreach function to display data :

        foreach($achieveDaily as $ad){
          $labels1[] = Carbon::parse($ad->tgl)->format('j');
          $plan1[] = $ad->plan;
          $actual1[] = $ad->actual;
        };

And here is the chartjs script :

<script>

var labelsL1    = JSON.parse('{!! json_encode($labels1) !!}');
var plan        = JSON.parse('{!! json_encode($plan1) !!}');
var actual      = JSON.parse('{!! json_encode($actual1) !!}');

const data = {
        labels: labelsL1,
        datasets: [{
            label: 'Plan',
            data: plan,
            backgroundColor: 'rgba(0, 0, 255, 0.8)',
            datalabels:{
                color: '#000000',
                anchor: 'end',
                align: 'top'
            },
        },
        {
            label: 'Actual',
            data: actual,
            backgroundColor: 'rgba(0, 255, 0, 1)',
            datalabels:{
                color: '#000000',
                anchor: 'end',
                align: 'top'
            },
        }]
    };

const config = {
    type: 'bar',
    data,
    plugins: [ChartDataLabels],
    options: {
        scales: {
            y: {
                beginAtZero: true
            }
        }
    }
}

const ctx = document.getElementById('dailyAchieveChart');
const dailyAchieve1 = new Chart(
    ctx,
    config
);

CodePudding user response:

You can use function whereMonth in laravel

DailyProduction::whereMonth('created_at', Carbon::now()->month)
            ->get();

CodePudding user response:

So if you want to display data by current Month, can use whereDate() function. Why NOT combine whereMonth() and whereYear() together?

They will run separate query for just filtering specific month, and specific year and combine it.

So better query = better performance. Here's example :

$dailyProduction = DailyProduction::whereDate('tgl', 'like', Carbon::now()->format('Y-m') . '%')->orderBy('tgl')->get();

$labels = [1, 2, ...]; // Basically anything to your graph labels, i assume you want to show daily
$plan = $actual = [];

foreach($dailyProduction as $daily) {
  $plan[] = $daily->plan;
  $actual[] = $daily->actual;
}

At your graph, when no data is there, just keep it blank. Or use blade @if @else

@if(empty($dailyProduction))
   <p>No Data</p>
@else
   // Your Graph
@endif

At your <script>. I assume rest of your const data is right

var labelsL1    = {!! json_encode($labels) !!};
var plan        = {!! json_encode($plan) !!};
var actual      = {!! json_encode($actual) !!};
  • Related