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

Laravel get single column value in laravel

How to get single column value in laravel ?

November 14, 2023March 16, 2024

In this guide, we’ll dive into get single column value in Laravel Eloquent ORM. In Laravel development, optimizing database queries is pivotal for creating efficient and high-performance applications. This technique is valuable for extracting targeted data from your database, enhancing the precision and speed of your application. To illustrate get…

Read More
Php Laravel ajax login and register

Laravel ajax login and registration with example

December 4, 2021November 12, 2023

In Laravel 8 there is multiple ways to implement the laravel Ajax login and registration like Laravel provides its own auth with packages Jetstream, passport,sanctum, breeze and fortify. These all packages are easy to install and configure but sometimes our application requirement and design patterns are different or we can…

Read More
Laravel 9 Collection Count and Check Empty Eloquent Collection in Laravel

Collection Count and Check Empty Eloquent Collection in Laravel

October 26, 2022March 16, 2024

In this article we will learn collection count and detect or check empty eloquent collection in laravel. Laravel collection is used to simplify the operations of array. Illuminate\Support\Collection library is used to handle the collection and arrays in laravel. Laravel heavily used collections in eloquent query builder to result the…

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