Retrive all rows from last month (Laravel + Eloquent)

前端 未结 4 1670
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 20:10

    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)

提交回复
热议问题