How to Get only records created today in Laravel

How to Get only records created today in Laravel

In this post we will give you information about How to Get only records created today in Laravel. Hear we will give you detail about How to Get only records created today in Laravel And how to use it also give you demo for it if it is necessary.

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', CarbonCarbon::today())->get(); // Eloquent

// or

DB::table('posts')->whereDate('created_at', CarbonCarbon::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.

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

  .       .

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

Hope this code and post will helped you for implement How to Get only records created today in Laravel. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US