How to Get Today Created Records in Laravel

Laravel get today created records example; In this tutorial we will show you how to get only the records that were created today in laravel application. We will give you simple and easy examples to get today created records in laravel.

Gettng only today date records we are will use whereDate methods and one other raw query. You can implement this example in your laravel 5, laravel 6, laravel 7, laravel 8 or laravel 9 application.

Laravel Get Today Created Records using whereDate

You can use the whereDate method with carbon to getting today date records in laravel application;

<?php

namespace App\Http\Controllers;

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

class PostController extends Controller
{
    public function index()
    {
        $posts = Post::whereDate('created_at', Carbon::today())->get();

        dd($posts);
    }
}

Laravel get Today records using Raw Query

You can get today data using the whereRaw query to with php date function in laravel project;

<?php

namespace App\Http\Controllers;

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

class PostController extends Controller
{
    public function index()
    {
        $posts = DB::table('posts')->whereRaw('Date(created_at) = CURDATE()')->get();

        dd($posts);
    }
}

I hope these example help you.

Leave a Comment