Search code examples
laravellaravel-8php-carbon

Laravel find records that exists in a CarbonPeriod


I created a CarbonPeroid object between 2 dates following this

$period = CarbonPeriod::create('2018-06-14', '2018-06-20');

Now I want to filter all those posts that exists in that $period object.

I tried like this but it doesn't work

        $posts = Posts::whereBetween(
            'created_at', [$period]
        )->get();

any help is appreciated.


Solution

  • Provide start and end date from your CarbonPeriod object.

    $posts = Posts::whereBetween(
            'created_at', [$period->startDate, $period->endDate]
        )->get();