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 max in eloquent query

Laravel max in eloquent query with example

January 23, 2022November 7, 2023

In this article we will learn to use aggregate function max in laravel eloquent and query builder. Laravel itself provide inbuilt method to max the columns like in MySQL or SQL based database we can do max of columns using max aggregate function. Laravel eloquent or query builder max method…

Read More
Php How to Update Enum Field Value using Laravel Migration

How to Update Enum Field Value using Laravel Migration ?

August 13, 2022March 16, 2024

Sometime after add the values in enum field we wanted to Update Enum Field Value using Laravel Migration so that we can work according to requirements. In our last article How to Create Enum Field in Laravel Migration ? we learnt to create the enum and today we are give…

Read More
Php Multiple Ways to Apply Unique Validation with Soft Delete in Laravel

Multiple Ways to Apply Unique Validation with Soft Delete in Laravel

June 17, 2022June 17, 2022

In this article we will solve the problem of Unique Validation with Soft Delete in Laravel. Soft delete trait in laravel by default add the delete_at null query to all queries but in validation rules of unique it doesn’t apply because validation rule does not use Laravel eloquent model and…

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

  • 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

  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
  • Understanding High Vulnerabilities: A Deep Dive into the Weekly Summary
  • Building a Million-Dollar Brand: The Journey of Justin Jackson
  • Mastering Schedule Management with Laravel Zap
  • The Resilience of Nature: How Forests Recover After Fires
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version