How to Get Only Records Created Today in Laravel - Techvblogs

How to Get Only Records Created Today in Laravel

Learn how to get only records created today in Laravel using easy queries to filter today's data efficiently.


Suresh Ramani - Author - Techvblogs
Suresh Ramani
 

2 days ago

TechvBlogs - Google News

Hey Folks, If you are working on large-scale Applications or any SAAS application and as of project requirement. You need to show some analytics in your project like Today, Yesterday, Last Month Data, Current Month Data, Last 3 Months Data, Last 6 Months Data and any other historical data. In this article, we will learn how to get only records created today in laravel.

In this article, We will use Carbon also Laravel use Carbon to get the DateTime.

What is Carbon in Laravel?

Carbon is a simple API extension of DateTime. Carbon makes it easy to get DateTime. Also, There are multiple methods to get data like specific timezone date, today date, yesterday, and many other methods. You can check it out.

How to Get only records created today in Laravel

Here are some examples of How to get current date records from the Database. You can use this example to get the current date record from the database.

Post::whereDate('created_at', \Carbon\Carbon::today())->get(); // Eloquent

// or

\DB::table('posts')->whereDate('created_at', \Carbon\Carbon::today())->get(); // Query Builder

// or

Post::whereDate('created_at', now()->today())->get(); // Eloquent

// or

\DB::table('posts')->whereDate('created_at', now()->today())->get(); // Query Builder

Some other examples of getting the current date record from the database.

Post::whereDate('created_at', \DB::raw('CURDATE()'))->get(); // Eloquent

// or

\DB::table('posts')->whereDate('created_at', \DB::raw('CURDATE()'))->get(); // Query Builder

This is a simple method to get the current date record from the database.

Output

[
  {
    'title': "How to Get only records created today in Laravel",
    'slug': "how-to-get-only-records-created-today-laravel",
    'created_at': "2022-09-01 23:21:27",
    
  },
  {
    'title': "How to Install Mautic ubuntu",
    'slug': "how-to-install-mautic-ubuntu",
    'created_at': "2022-09-01 23:14:08",
    
  }
]

Thank you for reading this blog.

Read Also: How to Get Data between Two Dates in Laravel

Comments (0)

Comment


Note: All Input Fields are required.