How to retrieve data of current date in Laravel
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Title: A Comprehensive Guide to Retrieving Data of Current Date in Laravel
Introduction: In this blog post, we will guide you through the process of retrieving data for today's date from your database using Laravel, a popular web application framework with expressive syntax. We will explore various methods and best practices to ensure efficient and accurate results.
Retrieving Data Using Eloquent Query Builder: The Eloquent Query Builder is an essential tool in Laravel for interacting with the database. To retrieve data of today's date, you can use the `whereDate` method as shown below:
PrescriptionTbl::whereDate('created_at', '=', Carbon::today())->get();
In this code snippet, we have used a custom date formatting for today's date (`Carbon::today()`) instead of a hardcoded value. This ensures that the query will pick up data created on today's date each time the application is executed. However, if you want to retrieve all records regardless of their creation date, simply remove the date condition from the query.
Using Carbon: Laravel provides an excellent library called Carbon for working with dates and times. You can use it to create a timestamp object representing today's date as follows:
$today = Carbon::now()->format('Y-m-d'); // Format output as '2019-02-19'
PrescriptionTbl::whereDate('created_at', '=', $today)->get();
Note that you should use the `Carbon::now()` method instead of a hardcoded date to ensure consistent results.
Considerations: When retrieving data based on the current date, keep in mind that the database query will only retrieve records created today at the time the application is executed. If your application runs multiple times throughout the day or you need to retrieve historical data for today's date, consider using a different approach. In such cases, you can use Carbon's `today()` method:
PrescriptionTbl::whereDate('created_at', '>=', Carbon::today()->startOfDay())->whereDate('created_at', '<=', Carbon::today()->endOfDay())->get();
This query will retrieve all records created today, regardless of the time they were actually created.
Using Date Functions: If you are not using the Eloquent Query Builder or want to use SQL queries directly, you can leverage Laravel's date functions for more complex cases. For example, you can use `date()` function with appropriate parameters:
$sql = "SELECT * FROM prescription_tbls WHERE DATE(`created_at`) = :today";
$results = DB::select($sql, ['today' => date('Y-m-d')]);
Conclusion: By following these methods and best practices, you can efficiently retrieve data for the current date in your Laravel application. Always use custom date formatting or appropriate functions to ensure that the query is up-to-date with today's date. Remember to test and refine your queries as per the requirements of your project to achieve accurate results.
For more information on working with dates in Laravel, visit https://laravelcompany.com/blog/working-with-dates-in-laravel.