How to Get only records created today in Laravel - TechvBlogs

How to Get only records created today in Laravel

In this article, You will learn How to Get only records created today in Laravel.


Suresh Ramani - Author - TechvBlogs
Suresh Ramani
 

1 month ago

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

If you want to manage your VPS / VM Server without touching the command line go and  Checkout this linkServerAvatar allows you to quickly set up WordPress or Custom PHP websites on VPS / VM in a  matter of minutes.  You can host multiple websites on a single VPS / VM, configure SSL certificates, and monitor the health of your server without ever touching the command line interface.

If you have any queries or doubts about this topic please feel free to contact us. We will try to reach you.

Comments (0)

ServerAvatar
Multiple sites on a single VPS/VM/Server Deploy PHP sites on cloud in minutes, simple as that!
ServerAvatar
Generate Free SSL Certificate - SSLForWeb

Generate Free SSL Certificate

  • 90-Day Certificates
  • SSL Monitoring
  • Wildcard Certificates
  • Multiple Domains
  • Certificate Management
  • Expiration Reminders

Comment


Note: All Input Fields are required.