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 Laravel pagination with search

How to use Laravel pagination with search ?

February 22, 2022November 19, 2023

In this tutorial we will learn pagination in Laravel. Laravel provides its own library to build the pagination html, which we can easily use in our html page using $model->links() method and $model->paginate() method to make a long list into pagination. Thus in this tutorial i will show you to…

Read More
Laravel How to get laravel application root path

How to get laravel application root path ?

October 13, 2022March 16, 2024

In this blog post, we’ve shown you how to get laravel application root path directory. If you need to get the path from anywhere in your application, you can use the Laravel public_path() helper function. If you need to get the path to your application’s storage directory, you can use…

Read More
Php How to Create or Replace File Content in Laravel

How to Create or Replace File Content in Laravel ?

May 29, 2022November 9, 2023

This article will guide you to Check File exist and Create or Replace File Content in Laravel. To perform the file system based operations like Check file exist and create or replace file in laravel. we use File class or Storage class in laravel. Laravel provides inbuilt library to access…

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

  • August 2025
  • July 2025
  • 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 Transformative Power of Education in the Digital Age
  • Understanding High Vulnerabilities: A Closer Look at the Week of July 14, 2025
  • Exploring Fresh Resources for Web Designers and Developers
  • The Intersection of Security and Technology: Understanding Vulnerabilities
  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version