How to get records between two date in Laravel using eloquent

2024-03-04 05:36:37 Mahi Laravel

Laravel eloquent between dates

We can get records between two dates laravel 5 with using Eloquent "whereBetween" method. Here sample function to get posts between two dates from the database.

  public function getPosts(){
     $fromDate = date('Y-m-d' . ' 00:00:00', time());
     $toDate   = date('Y-m-d' . ' 22:00:40', time());
     $posts = POST:: where('user_id',$this->user_id)->where('status',1)->whereBetween('posted_on',[$fromDate, $toDate])->get();
     return $posts;

This post is submitted by one of our members. You may submit a new post here.

Related Tricks