Laravel Get Records Between Two Dates

Published on Aug 31, 2020

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

Comments (0)