<?php use App\Model\Post; $from = date('2021-10-10'); $to = date('2021-10-20'); // Get all posts from 10th of October to 20th of October Post::query() ->whereBetween('created_at', [$from, $to]) ->get();
Using Carbon Instance
Besides using the "date()" method you can also use the Carbon instance.
<?php use Carbon\Carbon; use App\Model\Post; $from = Carbon::parse('2021-10-10'); $to = Carbon::parse('2021-10-20'); // Get all posts from 10th of October to 20th of October Post::query() ->whereBetween('created_at', [$from, $to]) ->get();
Getting Posts for Past 3 Months
You can also get the records for the past x months using the method mentioned above.
<?php // get posts record for the past 3 months $posts = Posts::query() ->whereBetween(now(), now()->subMonths(3)) ->get();
Leave a reply