Laravel Get Records Between Two Dates

Published on August 31, 2020 25 sec read

HIRE US 🖐
We're available to do freelance project. Take a look at our services!

Hello artisan, in this article, I’m going to share how to get all records between two dates from database using laravel eloquent method. Let’s see:

Table of Contents

  1. Example 1
  2. Example 2

Example 1

We’ll use whereBetween method. The whereBetween method verifies that a column’s value is between two values:

public function records(Request $request)
{
    $start_date = $request->start_date;
    $end_date = $request->end_date;

    $users = User::whereBetween('created_at',[$start_date,$end_date])->get();

    dd($users); 
}

Example 2

We can get record between two dates by using normal where condition:

public function records(Request $request)
{
    $start_date = $request->start_date;
    $end_date = $request->end_date;

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

    dd($users); 
}
That’s it. Thanks for reading. 🙂

Author

Hey, I'm Md Obydullah. I build open-source projects and write on Laravel, Linux server, modern JavaScript and more on web development.

Follow

Leave a Reply

Your email address will not be published. Required fields are marked *