Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrive all rows from last month (Laravel + Eloquent)

I'm trying to get all records that belongs to last month, so far I managed to get all from last month but to date today, I'm not sure how I can get only for last month

$revenueMonth = Callback::where('created_at', '>=', Carbon::today()->startOfMonth()->subMonth())->sum('payment');
like image 634
Klaus Avatar asked Sep 07 '17 12:09

Klaus


People also ask

How do I get my last month record from Laravel?

Laravel Get Last Month records Use the below laravel eloquent query to get the last month records from the database table. User::whereMonth('created_at', '=', Carbon::now()->subMonth()->month)->get(['name','created_at']); This query uses laravel method whereMonth() and get().

How do I get the latest 10 record in Laravel?

You may try something like this: $dogs = Dogs::orderBy('id', 'desc')->take(5)->get(); Use orderBy with Descending order and take the first n numbers of records.


1 Answers

Try this solutions:

$revenueMonth = Callback::where(
    'created_at', '>=', Carbon::now()->subDays(30)->toDateTimeString()
);

You get all Callback for last 30 days.

$revenueMonth = Callback::where(
    'created_at', '>=', Carbon::now()->firstOfMonth()->toDateTimeString()
);

Get for current month.

$revenueMonth = Callback::where(
    'created_at', '>=', Carbon::now()->startOfMonth()->subMonth()->toDateString()
);

Get for start last month.

UPDATED

$revenueMonth = Callback::where(
    'created_at', '>=', Carbon::now()->subMonth()->toDateTimeString()
);

This is what are you looking for :)

Hope it will help you:)

like image 113
Odin Thunder Avatar answered Oct 12 '22 04:10

Odin Thunder