Retrive all rows from last month (Laravel + Eloquent)
Asked Answered
H

4

23

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');
Hardden answered 7/9, 2017 at 12:23 Comment(3)
are you getting list of records of previous month with this query?Cleliaclellan
I'm getting all records from start last month to todays date, so example: 2017-08-01 -> 2017-09-07 instead of 2017-08-01 -> 2017-08-31Hardden
so what you want let me know so that I can helpCleliaclellan
L
21

More clear solution for your problem:

$revenueMonth = Callback::whereMonth(
    'created_at', '=', Carbon::now()->subMonth()->month
);
Laticialaticiferous answered 7/9, 2017 at 13:46 Comment(1)
Just want to point out that this will take the last month integer and return records from all years matching that month. Let's say it's November, Carbon would return "10". So, this query would match on created_at = '2018-10-01 12:23:34' AND it would also match on created_at = '2015-10-10 13:24:35'... not just the last month. The SQL it returns is basically where month(created_at) = 10.Rochester
L
10

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:)

Laticialaticiferous answered 7/9, 2017 at 12:33 Comment(3)
I'm not looking for the last 30 days for rather for the last month, so example: 2017-08-01 -> 2017-08-31 instead of 2017-08-24 -> 2017-09-7 :)Hardden
$revenueMonth = Callback::where( 'created_at', '>=', Carbon::now()->startOfMonth()->subMonth()->toDateString() ); That returns: 2017-09-07 09:46:43 2017-09-07 09:46:43 2017-09-07 09:46:43 2017-08-07 09:46:43 2017-09-02 09:46:43 And I'm only looking for 2017-08-07 09:46:43Hardden
Hmm still not working as I want :P. It returns 2017-09-07 09:46:43 2017-09-07 09:46:43 2017-09-07 09:46:43 2017-09-02 09:46:43 And I'm looking for it to return the record of: 2017-08-07 09:46:43Hardden
C
6

UPDATED eloquent version of the your answer

$fromDate = Carbon::now()->subMonth()->startOfMonth()->toDateString();
$tillDate = Carbon::now()->subMonth()->endOfMonth()->toDateString();

$revenueLastMonth = Callback::whereBetween('created_at',[$fromDate,$tillDate])->get();
Cleliaclellan answered 7/9, 2017 at 12:36 Comment(4)
$revenueMonth = Callback::where( 'created_at', '>=', Carbon::today() ->startOfMonth()->subMonth(1)->toDateTimeString()); That returns: 2017-09-07 09:46:43 2017-09-07 09:46:43 2017-09-07 09:46:43 2017-08-07 09:46:43 2017-09-02 09:46:43 And I'm only looking for 2017-08-07 09:46:43Hardden
I have edited my answer check it and if still doesn't work then let me know :)Cleliaclellan
It still returns everything from 2017-08-07 09:46:43 to 2017-09-07 09:46:43, I have added an answer myself, but I think it could be made in a different way, just don't know how :)Hardden
yeah I have edited your and my answer try the eloquent way as suggested in docsCleliaclellan
H
4

None of the answers get's me to where I'm looking to go :(.

I have a solution but I think it's ugly and hoped it could be made more clean

$fromDate = Carbon::now()->subMonth()->startOfMonth()->toDateString();
$tillDate = Carbon::now()->subMonth()->endOfMonth()->toDateString();

$revenueLastMonth = Callback::whereBetween(DB::raw('date(created_at)'), [$fromDate, $tillDate])->get();

This will give my the result I'm looking for, here is my records:

2017-09-07 09:46:43
2017-09-07 09:46:43
2017-09-07 09:46:43
2017-09-02 09:46:43
2017-08-07 09:46:43

And I want it to return ONLY what records is made in August 2017 (2017-08-07 09:46:43)

Hardden answered 7/9, 2017 at 13:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.