6

I want to retrieve data of today from my database in Laravel.

I have already googling and applied several methods but not getting my answer.

PrescriptionTbl::whereDate('created_at', '=', $today)->get();

Where PrescriptionTbl is the model and today is current date like 2019-2-19.But got NULL array.

To check the SQL I have used toSQL() instead of get()...and it shows below SQL in the browser

 select * from `prescription_tbls` where date(`created_at`) = ?

I have also use Carbon but also get a NULL array

PrescriptionTbl::whereDate('created_at', Carbon::today())->get();

How to solve it? Anybody Help Please?

5
  • Did you try PrescriptionTbl::where('created_at', date('Y-m-d H:i:s'))->get(); ? Commented Feb 19, 2019 at 6:59
  • FGDeveloper...Actually I want to retrieve data Date wise....That's why didn't use H:i:s Commented Feb 19, 2019 at 7:07
  • You can use that; PrescriptionTbl::whereDate('created_at', date('Y-m-d'))->get(); It works for me. Commented Feb 19, 2019 at 7:11
  • Can you please share the date format used by the fileld so that i can modify my function and post the answer and dont' forget to add some example dates Commented Feb 19, 2019 at 7:20
  • FGDeveloper....it works according to your code...Please provide the answer and I will accept your answer then Commented Feb 19, 2019 at 7:24

7 Answers 7

12

You can use that;

PrescriptionTbl::whereDate('created_at', date('Y-m-d'))->get();
Sign up to request clarification or add additional context in comments.

Comments

2

You can get it via new DateTime() class

PrescriptionTbl::whereDate('created_at', '=', (new DateTime)->format('Y-m-d'))->get();

Comments

1

Using Carbon ( use Carbon\Carbon; ) its simple and pretty

PrescriptionTbl::whereDate('created_at',  Carbon::today()->toDateString())->get();

Comments

0

You can try this one:

PrescriptionTbl::where('created_at', '<=', date('Y-m-d 00:00:00'))->get();

1 Comment

Sahil Darji.... I want to retrieve data as Date wise.....And in my database created_at column format is DATETIME
0

created_at is a timestamp which is date/time format, so, equal operator wouldn't work unless you provided the whole timestamp.

you can use whereDate method to compare dates only as mentioned in documentation

PrescriptionTbl::whereDate('created_at', $today)->get();

1 Comment

it shows select * from prescription_tbls` where created_at > ? and created_at = ?` after using toSql() and also get NULL result
0

You can use whereBetween in which you can provide start date of today as 2019-01-01 00:00:00 and end date as 2019-01-01 23:59:59

For reference How to query between two dates using Laravel and Eloquent ORM?

Comments

0

Another way to get the exact record of today's transactions is by using the like operator in conjunction with Carbon for example in Laravel 9+:

<?php

$date = Carbon::now();

    $todaySales = Sales::query()
        ->where("created_at", "like", "%{$date->today()->toDateString()}%")
        ->get();

    dd($todaySales);

The above will return an array of records for today.

Now why do we use this approach? I want you to take a good look at the created_at column field this is how it looks like: 2023-04-14 11:33:23. With this using where statement the result will be an empty array why? Because where statement uses an = operator by default which means the sql statement will attempt to match the records exactly the way it is. Here is how the query will look like:


SELECT * FROM sales WHERE created_at={$todaysDate}
// in this case todays date is 2023-04-14 
// but the value in the database is rather containing time 
// e.g.: 023-04-14 11:33:23 so definitely the select operation will fail and return empty results.

Looking at the above query there's no way the sql query is going to evaluate to any result but with the like operator the sql query will attempt to search for records that has todays date in them: 2023-04-14 which will return all records for today. If you're looking to retrieve records from a range let's say from Monday through to Friday you can use Laravel whereBetween() method otherwise try this approach. Hope this help you solve the problem.

You can also checkout this article for more info

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.