Laravel Get Records Between Two Dates

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. ?

Software Engineer | Ethical Hacker & Cybersecurity...

Md Obydullah is a software engineer and full stack developer specialist at Laravel, Django, Vue.js, Node.js, Android, Linux Server, and Ethichal Hacking.