Search code examples
laravelapilumen

How to display data based on a predefined date


$data_bulan = Bast::whereYear('tgl_bast', date('Y'))->whereMonth('tgl_bast', date('m'))->get();
if ($data_bulan) {
    return response()->json([
        'status'  => true,
        'message' => 'Data ditemukan',
        'data'    => [
            'data_mingguan' => [],
            'data_bulan' => $data_bulan
        ]
    ]);
} else {
    return response()->json([
        'status'  => false,
        'message' => 'Data tidak ditemukan',
        'data'    => []
    ]);
}

Here I have data whose data is date_bast, now I have found a way to retrieve data based on this month and the data will be lost if it has passed this month, well here I don't know how to display this week's data, for example tgl_bast ends in 2021-01- 25 So before the 25th, 7 days before the 25th the data appears until after the 25th the data doesn't appear anymore.


Solution

  • first you take the current date data, after that second you take the current date data plus 7 days, then you can use whereBetween in your query model.

    $a = date('Y-m-d');
    $b = date('Y-m-d',strtotime('+7 days'));
    $range = [$a, $b];
            
    $data_mingguan = Bast::whereBetween('tgl_bast', $range->orderBy('tgl_bast','asc')->get();