How to get Today Created Data in Laravel 11 |
How to get Today Created Data in Laravel 11 |

How to get Today Created Data in Laravel 11


In Laravel, retrieving data created today is a common task in many applications. Whether you’re building a blog, an e-commerce platform, or a social media application, being able to fetch today’s data efficiently is crucial. In this tutorial, we’ll walk through the process of fetching today’s created data in Laravel 11 step by step, accompanied by practical code examples. Let’s dive in!

Step 1:

Set Up Your Laravel Environment Before we begin, ensure you have Laravel 11 installed on your system. If not, you can install it via Composer using the following command:

composer create-project --prefer-dist laravel/laravel:^11.0 your-project-name

Navigate to your project directory:

cd your-project-name

Step 2:

Define Your Model For the sake of this tutorial, let’s assume we have a Post model and a corresponding posts table in our database. We want to fetch posts created today. If you don’t have a Post model, create one using the following artisan command:

php artisan make:model Post -m

This command will generate the Post model and its migration file.

Step 3:

Add Created_at Column to Migration In your migration file (create_posts_table.php), make sure you have a created_at column. If not, add it like so:

Schema::create('posts', function (Blueprint $table) {
$table->timestamps(); // This will add created_at and updated_at columns

Step 4:

Run Migration Run the migration to create the posts table in your database:

php artisan migrate

Step 5:

Fetch Today’s Created Data
Now, let’s retrieve posts created today. Open your controller where you want to fetch the data (e.g.,
PostController.php). In your method, you can use the whereDate method provided by Laravel’s Eloquent ORM to filter records based on a specific date. Here’s how you can do it:

use App\Models\Post;
use Illuminate\Http\Request;

public function index()
$todayPosts = Post::whereDate('created_at', today())->get();

return view('posts.index', compact('todayPosts'));

Step 6:

Display the Data Finally, let’s display the fetched data in your view file (index.blade.php):

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Today's Posts</title>
<h1>Today's Posts</h1>

@foreach($todayPosts as $post)
<li>{{ $post->title }}</li>

Step 7:

Test Your Application That’s it! You’ve successfully implemented fetching today’s created data in Laravel 11. Now, run your Laravel application using the following command:

php artisan serve

Open your browser and navigate to http://localhost:8000/posts (assuming posts is your route). You should see a list of posts created today.


In this tutorial, we’ve covered how to fetch today’s created data in Laravel 11 using Eloquent ORM. By following the steps outlined above, you can efficiently retrieve records created on the current date in your Laravel applications. This functionality can be useful in various scenarios, such as generating daily reports, displaying today’s activities, or implementing time-sensitive features. Happy coding!