Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Get Today Records in Laravel

How to Get Today Records in Laravel ?

Aman Jain, November 17, 2023March 16, 2024

In this blog post we will learn to get today records in laravel. we will use carbon and whereDate method of laravel eloquent to fetch the today records. In laravel models save the data in column created_at and created_at column stores the date time but if we want to fetch the records for today then it means we need to remove the time from created_at field so to do that we will use laravel eloquent whereDate method.

whereDate method parse the date time to date then we use Carbon::today() to get the date of today.

Table of Contents

Understand Get Today Records in Laravel with Example
Method 1: Using Eloquent’s whereDate Method
Method 2: Leveraging Raw SQL Queries
Method 3: Customizing with Scope Methods
Conclusion

Understand Get Today Records in Laravel with Example

To begin, let’s create a model. Open your terminal and run the following command

php artisan make:model Article

This command generates a new model named Record in the app directory.

Method 1: Using Eloquent’s whereDate Method

Laravel’s Eloquent provides a convenient whereDate method to filter records based on a specific date. To get today’s records, you can use this method in the controller or wherever you handle your data retrieval logic.

use App\Models\Article;
use Carbon\Carbon;

public function getTodayRecords()
{
    $todayRecords = Article::whereDate('created_at', Carbon::today())->get();
    // Process $todayRecords as needed
}

In this example, we’re querying the Article model, filtering records where the created_at date is equal to today’s date.

Method 2: Leveraging Raw SQL Queries

For scenarios where you prefer raw SQL queries, Laravel allows you to execute them seamlessly. Here’s an example:

use Illuminate\Support\Facades\DB;

public function getTodayRecords()
{
    $todayRecords = DB::select("SELECT * FROM articles WHERE DATE(created_at) = CURDATE()");
    // Process $todayRecords as needed
}

This method uses the DB::select method to execute a raw SQL query that retrieves records where the date matches the current date.

Method 3: Customizing with Scope Methods

To enhance code readability and reusability, consider implementing a scope method in your Eloquent model.

// In the Record model

public function scopeTodayArticles($query)
{
    return $query->whereDate('created_at', Carbon::today());
}

Now, you can effortlessly fetch today’s records in your controller:

$todayArticles = Article::scopeTodayArticles()->get();

Conclusion

In this guide, we explored multiple ways to get today records in Laravel. Whether you prefer Eloquent’s built-in methods, raw SQL queries, or custom scope methods, Laravel provides the flexibility to suit your coding style. Incorporate these techniques into your projects to streamline the process of fetching records based on the current date.

Remember, the key is understanding your project’s requirements and choosing the method that aligns best with your development workflow. Now, armed with this knowledge, you can efficiently retrieve today’s records in Laravel for a smoother and more productive coding experience.

Related

Laravel laraveltoday

Post navigation

Previous post
Next post

Related Posts

Php get last insert id in laravel

How to get last insert id in laravel with example ?

March 5, 2022February 22, 2024

In this article i will learn you to get last insert id in laravel, In laravel we can execute our queries in two ways first is using the laravel eloquent and other one is DB builder. so in the both pattern we can get the last inserted i in laravel….

Read More
Php How to use conditional validation Laravel 8

How to use conditional validation Laravel 8 / 9 ?

December 17, 2021March 19, 2022

In Laravel sometime you want to exclude some validations on specific condition. Laravel provides exclude_if, exclude_unless and sometimes validation rules to validating some rules conditionally. In this tutorial we will take a simple example of has_city is checked then city name should be require and if its not checked then…

Read More
Php How to fetch Soft Deleted Records in Laravel 9

How to Fetch Soft Deleted Records in Laravel 9 ?

June 15, 2022June 15, 2022

In this article we will learn to fetch soft deleted records in Laravel. In our recent article Use Soft Delete to Temporary (Trash) Delete the Records in Laravel 9 we learnt to delete the file without actually deleting from database and sometimes we want to show records that are soft…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version