首页 > 解决方案 > 如何使用 laravel eloquent 和 Carbon 获得将在下个月开始而不是在本月开始的课程?

问题描述

我有以下课程表:

------
courses
--------------------------------
  id            starting_date
-------         -------------
   1             2020-10-05
   2             2020-11-15
   3             2020-12-01
   4             2020-12-20
----------------------------------
   

我想检索下个月开始的课程。

$courses = Course::where('status',1)
     ->where('starting_date','>=', Carbon::now()->addMonth())
     ->orderBy('starting_date','desc')->get();

问题是我的查询检索了本月和下个月的记录,而我没有检索从本月开始的课程。

标签: phplaraveleloquent

解决方案


您可以使用 Carbon 获得下个月的第一天:

  $nextMonthFirstDay=(new  Carbon('first day of next month'))->startOfDay();


        $courses = Course:: where('status',1)
            ->where("starting_date",'>=',$nextMonthFirstDay)
          
            ->orderBy('starting_date','desc')->get();

推荐阅读