Get All Records Between Two Dates in Laravel
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...
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial