Hello artisan,
In this example, I will explain with source code to you how to get last 12 months data in laravel. I will use carbon to show you the way of fetching last 12 months data in laravel. If you don't know how to fetch last 12 months data using carbon in laravel then this example is for you.
I will show you an eloquent query using carbon to fetch last 12 months data. Sometimes we need to show that creates a report of last 12-month sell report. You know that we need that sometimes to create that kind of report. So Let's see the example query of the last 12 months data. I will use whereBetween
to fetch the last 12 months datarecords in laravel.
Example code:
Route::get('/', function () {
$last_three_month = Carbon::now()->startOfMonth()->subMonth(12);
$this_month = Carbon::now()->startOfMonth();
$data = DB::table('orders')
->whereBetween('placed_at',[$last_three_month,$this_month])
->get();
});
Read aslo: How to Get Last 3 Months Data in Laravel
Hope it can help you.
#laravel #eloquent-tips