Skip to content
Advertisement

How to get courses that will start next moths and not starting in the current month using laravel eloquent and Carbon?

I have the following courses table :

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

I want to retrieve the courses that will start next months.

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

The problem is that my query retrieves the records of this month and next months records and I don’t to retrieve courses starting at this month.

Advertisement

Answer

you can get the first of the next month using Carbon:

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


        $courses = Course:: where('status',1)
            ->where("starting_date",'>=',$nextMonthFirstDay)
          
            ->orderBy('starting_date','desc')->get();
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement