Retrive all rows from last month (Laravel + Eloquent)

Klaus picture Klaus · Sep 7, 2017 · Viewed 17.9k times · Source

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');

Answer

Odin Thunder picture Odin Thunder · Sep 7, 2017

More clear solution for your problem:

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