Get Only Records Created Today in Laravel with Example

In this tutorial, we will show you how to get only records created today in Laravel. We will use the whereDate() method of the Eloquent model to filter the results to only those records where the created_at column is today's date. This is a useful technique for getting analytics on data that was created recently, such as new user registrations or product purchases.

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",
    
  }
]

 Happy Coding !!!

#laravel #php 

Get Only Records Created Today in Laravel with Example
1.15 GEEK