How to Get Last 12 Months Data in Laravel?

About Us:

We are from free coder. We are a group of web developers who are passionate about web development. You can learn from us php, laravel, node js, vue js, react js and many other topic on web developement.
we are very happy to help you.
free coder

Today what you are going to learn:

Hello Brother,

This tutorial will provide lesson of how to get last 12 months data in laravel. step by step explain laravel get last 12 months records. you will learn laravel get last one year data. This article will give you simple lesson of laravel get last 12 months data. You just need to some step to done get last 12 months data in laravel.

we can get last 12 months records in laravel 6, laravel 7 and laravel 8 application.

Hello Brother, i will give you very simple lesson of how to get last 12 months data in laravel. we will use whereBetween() and Carbon in this lesson.

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()->subYear(), Carbon::now()]

)

->get();

dd($items);

}

}

Result:-

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] => 10

[title] => Dr.

[body] => Body 3

[created_at] => 2021-04-01T09:27:53.000000Z

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

)

[2] => Array

(

[id] => 12

[title] => Dr.

[body] => Body 2

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

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

)

[3] => Array

(

[id] => 13

[title] => Mrs.

[body] => Body 1

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

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

)

)

thank you very much for following up with me.

Tags

Recent Posts

Related Posts