How to Get Last 6 Months Data in Laravel?

By Hardik Savani April 16, 2024 Category : Laravel

Hi,

This is a short guide on how to get last 6 months data in laravel. you can see laravel get last 6 months records. we will help you to give example of laravel get last 6 months data. i explained simply step by step get last six months data in laravel. Let's see bellow example get last 6 months data in laravel.

we can get last 6 months records in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 application.

Here, i will give you very simple example of how to get last 6 months data in laravel. we will use whereBetween() and Carbon in this example.

I have one created items table that structure as bellow you can see.

items table:

Controller Code:

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Http;

use App\Models\Item;

use Carbon\Carbon;

class ITSController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$items = Item::select('*')

->whereBetween('created_at',

[Carbon::now()->subMonth(6), Carbon::now()]

)

->get();

dd($items);

}

}

Output:

Array

(

[0] => Array

(

[id] => 9

[title] => Dr.

[body] => Body 4

[created_at] => 2021-06-07T18:13:40.000000Z

[updated_at] => 2021-08-12T13:46:08.000000Z

)

[1] => Array

(

[id] => 12

[title] => Dr.

[body] => Body 2

[created_at] => 2021-09-07T13:46:43.000000Z

[updated_at] => 2021-08-12T13:46:43.000000Z

)

[2] => Array

(

[id] => 13

[title] => Mrs.

[body] => Body 1

[created_at] => 2021-09-01T13:46:43.000000Z

[updated_at] => 2021-08-12T13:46:43.000000Z

)

)

I hope it can help you...

Tags :
Shares