How to Get Last Month Data in Laravel?
Hi,
This article will give you example of how to get last month data in laravel. This article will give you simple example of how to get previous month in laravel. In this article, we will implement a how to get last month record in laravel. Here you will learn laravel get last month records.
we can get last month 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 month 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()->startOfMonth(), Carbon::now()->subMonth()->endOfMonth()]
)
->get()
->toArray();
dd($items);
}
}
Output:
Array
(
[0] => Array
(
[id] => 9
[title] => Dr.
[body] => Body 4
[created_at] => 2021-09-14T18:13:40.000000Z
[updated_at] => 2021-08-12T13:46:08.000000Z
)
[1] => Array
(
[id] => 10
[title] => Dr.
[body] => Body 3
[created_at] => 2021-09-30T09: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
)
)
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Get Month Wise Data in Laravel?
- How to Get Today Created Records in Laravel?
- How to Get Current Week Records in Laravel?
- Laravel Eloquent without() and withOnly() Method Example
- How to Restore Deleted Records in Laravel?
- How to Get Last 7 Days Record in Laravel?
- How to Get Last 30 Days Record in Laravel?
- Delete All Records from Table in Laravel Eloquent
- How to Get Soft Deleted Records in Laravel?
- How to Insert Multiple Records in Laravel?
- How to Check If Record Exists or Not in Laravel?