Get All Records Between Two Dates in Laravel

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share
Get All Records Between Two Dates in Laravel

Hello Dev’s,
Now today on this blog, I will share with you a example of how to get all records between two dates from database by using laravel. Here we will use laravel Eloquent whereBetween method in our laravel application.

So now here i am getting all records from our users table and get all records from start date and end date between records. So now we can easily do this simply way to get records between two dates.

Now in Laravel application whereBetween method are more useful when we retrieve records in between two given dates from database.

Solution 1 :
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function getRecord(Request $request)
{
    $users = User::whereBetween('created_at',[$request->start_date,$request->end_date])
    ->get();

    dd($users); 
}
Solution 2 :
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function getRecord(Request $request)
{

    $users = User::where('created_at','>=',$request->start_date)
        ->where('created_at','<=',$request->end_date)
        ->get();

    dd($users); 

}
Solution 3 :
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function getRecord(Request $request)
{
    
    $users = User::select("users.*")
        ->whereBetween('created_at', [$request->start_date, $request->end_date])
        ->get();

    dd($users); 
}

Output:

Illuminate\Database\Eloquent\Collection {#269 ?
  #items: array:5 [?
    0 => App\User {#270 ?}
    1 => App\User {#271 ?}
    2 => App\User {#272 ?}
    3 => App\User {#273 ?}
    4 => App\User {#274 ?}
  ]
}

Read Also : Angularjs PHP MySQL Pagination Example

I hope it will help you. Also you can follow us on Facebook

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →