[英]Get only records created today in laravel
How do I use the created_at
field to get only the records that were created today and no other day or time?如何使用created_at
字段仅获取今天创建的记录,而不是其他日期或时间?
I was thinking of a ->where('created_at', '>=', Carbon::now())
But Im not sure that would work.我在想->where('created_at', '>=', Carbon::now())
但我不确定这是否可行。
对于 Laravel 5.6+ 用户,你可以这样做
$posts = Post::whereDate('created_at', Carbon::today())->get();
Use Mysql
default CURDATE
function to get all the records of the day.使用Mysql
默认的CURDATE
函数获取当天的所有记录。
$records = DB::table('users')->select(DB::raw('*'))
->whereRaw('Date(created_at) = CURDATE()')->get();
dd($record);
Note笔记
The difference between Carbon::now
vs Carbon::today
is just time. Carbon::now
与Carbon::today
之间的区别只是时间。
eg例如
Date printed through Carbon::now
will look like something:通过Carbon::now
打印的日期看起来像这样:
2018-06-26 07:39:10.804786 UTC (+00:00)
While with Carbon::today
:使用Carbon::today
:
2018-06-26 00:00:00.0 UTC (+00:00)
To get the only records created today with now
can be fetched as:要获取今天创建的唯一记录, now
可以获取为:
Post::whereDate('created_at', Carbon::now()->format('m/d/Y'))->get();
while with today
:而today
:
Post::whereDate('created_at', Carbon::today())->get();
UPDATE更新
As of laravel 5.3, We have default where clause whereDate / whereMonth / whereDay / whereYear
从 Laravel 5.3 开始,我们有默认的 where 子句whereDate / whereMonth / whereDay / whereYear
$users = User::whereDate('created_at', DB::raw('CURDATE()'))->get();
OR with DB
facade或与DB
门面
$users = DB::table('users')->whereDate('created_at', DB::raw('CURDATE()'))->get();
Usage of the above listed where clauses上面列出的 where 子句的用法
$users = User::whereMonth('created_at', date('m'))->get();
//or you could also just use $carbon = \Carbon\Carbon::now(); $carbon->month;
//select * from `users` where month(`created_at`) = "04"
$users = User::whereDay('created_at', date('d'))->get();
//or you could also just use $carbon = \Carbon\Carbon::now(); $carbon->day;
//select * from `users` where day(`created_at`) = "03"
$users = User::whereYear('created_at', date('Y'))->get();
//or you could also just use $carbon = \Carbon\Carbon::now(); $carbon->year;
//select * from `users` where year(`created_at`) = "2017"
If you are using Carbon (and you should, it's awesome!) with Laravel, you can simply do the following:如果你在 Laravel 中使用 Carbon(你应该这样做,它太棒了!),你可以简单地执行以下操作:
->where('created_at', '>=', Carbon::today())
Besides now()
and today()
, you can also use yesterday()
and tomorrow()
and then use the following:除了now()
和today()
,您还可以使用yesterday()
和tomorrow()
,然后使用以下内容:
startOfDay()
/ endOfDay()
startOfDay()
/ endOfDay()
startOfWeek()
/ endOfWeek()
startOfWeek()
/ endOfWeek()
startOfMonth()
/ endOfMonth()
startOfMonth()
/ endOfMonth()
startOfYear()
/ endOfYear()
startOfYear()
/ endOfYear()
startOfDecade()
/ endOfDecade()
startOfDecade()
/ endOfDecade()
startOfCentury()
/ endOfCentury()
startOfCentury()
/ endOfCentury()
with carbon:含碳:
return $model->where('created_at', '>=', \Carbon::today()->toDateString());
without carbon:无碳:
return $model->where('created_at', '>=', date('Y-m-d').' 00:00:00');
You can use whereRaw('date(created_at) = curdate()')
if timezone is not a concern or whereRaw('date(created_at) = ?', [Carbon::now()->format('Ym-d')] )
otherwise.如果时区不是问题,您可以使用whereRaw('date(created_at) = curdate()')
或whereRaw('date(created_at) = ?', [Carbon::now()->format('Ym-d')] )
否则。
Since the created_at
field is a timestamp, you need to get only the date part of it and ignore the time part.由于created_at
字段是一个时间戳,因此您只需要获取其中的日期部分而忽略时间部分。
No need to use Carbon::today
because laravel uses function now() instead as a helper function无需使用Carbon::today
因为 laravel 使用函数 now() 作为辅助函数
So to get any records that have been created today you can use the below code:因此,要获取今天创建的任何记录,您可以使用以下代码:
Model::whereDay('created_at', now()->day)->get();
You need to use whereDate
so created_at will be converted to date.您需要使用whereDate
这样 created_at 将被转换为日期。
$today = Carbon\Carbon::now()->format('Y-m-d').'%';
->where('created_at', 'like', $today);
Hope it will help you希望它会帮助你
For readability purposes i use query scope , makes my code more declarative.出于可读性目的,我使用查询范围,使我的代码更具声明性。
namespace App\Models;
use Illuminate\Support\Carbon;
use Illuminate\Database\Eloquent\Model;
class MyModel extends Model
{
// ...
/**
* Scope a query to only include today's entries.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeCreatedToday($query)
{
return $query->where('created_at', '>=', Carbon::today());
}
// ...
}
MyModel::createdToday()->get()
Sql : select * from "my_models" where "created_at" >= ?
Bindings : ["2019-10-22T00:00:00.000000Z"]
简单的解决办法:
->where('created_at', 'like', date("Y-m-d")."%");
I've seen people doing it with raw queries, like this:我见过有人用原始查询来做这件事,像这样:
$q->where(DB::raw("DATE(created_at) = '".date('Y-m-d')."'"));
Or without raw queries by datetime, like this:或者不按日期时间进行原始查询,如下所示:
$q->where('created_at', '>=', date('Y-m-d').' 00:00:00'));
Luckily, Laravel Query Builder offers a more Eloquent solution:幸运的是,Laravel Query Builder 提供了一个更 Eloquent 的解决方案:
$q->whereDate('created_at', '=', date('Y-m-d'));
Or, of course, instead of PHP date() you can use Carbon :或者,当然,您可以使用Carbon代替 PHP date() :
$q->whereDate('created_at', '=', Carbon::today()->toDateString());
It's not only whereDate.这不仅是 whereDate。 There are three more useful functions to filter out dates:还有三个更有用的函数可以过滤掉日期:
$q->whereDay('created_at', '=', date('d'));
$q->whereMonth('created_at', '=', date('m'));
$q->whereYear('created_at', '=', date('Y'));
Below code worked for me下面的代码对我有用
$today_start = Carbon::now()->format('Y-m-d 00:00:00');
$today_end = Carbon::now()->format('Y-m-d 23:59:59');
$start_activity = MarketingActivity::whereBetween('created_at', [$today_start, $today_end])
->orderBy('id', 'ASC')->limit(1)->get();
Carbon::today() will return something like this: 2021-08-06T00:00:00.000000Z
, so using Model::where('created_at', Carbon::today())
will only return records created at exactly 12:00 am current date. Carbon::today() 将返回如下内容: 2021-08-06T00:00:00.000000Z
,因此使用Model::where('created_at', Carbon::today())
将只返回恰好在 12 处创建的记录:当前日期上午 00 点。
Use Model::where('created_at', '>=', Carbon::today())
instead使用Model::where('created_at', '>=', Carbon::today())
代替
It will give you All the posts created today !!!!!它会给你今天创建的所有帖子!!!!!! if you use time with this you will get posts of that particular time not of today如果你用这个时间,你会得到那个特定时间而不是今天的帖子
laravel 8 laravel 8
$VisitorEntryStatusDateCurrent = VisitorEntry::whereDate('created_at', Carbon::today())->get();
$records = User::where('created_at' = CURDATE())->GET()); $records = User::where('created_at' = CURDATE())->GET()); print($records);打印($记录);
I use laravel9 on 22 Apr 2022 how I get the "today" record is:我在 2022 年 4 月 22 日使用 laravel9,我如何获得“今天”记录是:
I have edit "config/app.php" on the "timezone" (about line 72 ) I have set it to my timezone which is "Asia/Bangkok"我已经在“时区”上编辑了“config/app.php”(大约第 72 行)我已将其设置为我的时区,即“亚洲/曼谷”
my query code I have is:我的查询代码是:
$get = User::whereDate("created_at","=",date("Ymd",time() ) )->get(); $get = User::whereDate("created_at","=",date("Ymd",time() ) )->get();
will get the field that created today.将获得今天创建的字段。
I don't know if this a correct way or it another bad code but as long as it work for me I will be okay.我不知道这是正确的方法还是另一个错误的代码,但只要它对我有用,我会没事的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.