Get All Records Between Two Dates in Laravel

Laravel 6 , Laravel , Laravel 7

Nicesnippets

12127

24-12-2019


Hi Dev,

Today, I will learn you example of get all records between two dates from database using laravel Eloquent whereBetween method in laravel.

Here i am getting records from users table and get all records from start date and end date between records. So you can easily ans simply way to get records between two dates.

Laravel whereBetween method are useful when you 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 ?}

]

}

It will help you...


Recommended Posts