Hello artisan,
In this example, I will show you how to fetch last 3 months data in laravel. I will use carbon to show you the way of fetching last 3 months data in laravel. If you don't know how to fetch last 3 months data using carbon in laravel then this example is for you.
Let assume we have a date
column in our table and we have to fetch last 3 months data from this table. How we can write this query to fetch. I will show you an eloquent query using carbon to fetch last 3 months data.
Sometimes we need to show that kind of data to make a report in our application. So Let's see the example query of last 3 months data. I will use whereBetween
to fetch last 3 months datarecords in laravel.
Example code:
Route::get('/', function (\Illuminate\Http\Request $request) {
$last_three_month = Carbon::now()->startOfMonth()->subMonth(3);
$this_month = Carbon::now()->startOfMonth();
$data = DB::table('orders')
->whereBetween('placed_at',[$last_three_month,$this_month])
->get();
});
Read also: Get Last 30 Days Records From Database In Laravel
Hope it can help you.
#laravel #eloquent-tips