Retrive all rows from last month (Laravel + Eloquent)

前端 未结 4 1675
Happy的楠姐
Happy的楠姐 2021-01-11 19:21

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

4条回答
  •  爱一瞬间的悲伤
    2021-01-11 19:47

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

提交回复
热议问题