Php Laravel Carbon返回我0个结果

Php Laravel Carbon返回我0个结果,php,laravel,eloquent,php-carbon,Php,Laravel,Eloquent,Php Carbon,我需要为数据库中的用户获取这个月的数据,所以我写: dd($user->products->where('paid',1)->where('created_at','>=', Carbon::now()->startOfMonth()->toDateString())->all()); 我得到了0个结果 当我尝试Carbon::now()->subMonth()时效果很好,但最后30天返回我 我怎样才能解决这个问题?如何仅获取本月数据?->toDat

我需要为数据库中的用户获取这个月的数据,所以我写:

 dd($user->products->where('paid',1)->where('created_at','>=', Carbon::now()->startOfMonth()->toDateString())->all());
我得到了0个结果

当我尝试
Carbon::now()->subMonth()
时效果很好,但最后30天返回我

我怎样才能解决这个问题?如何仅获取本月数据?

->toDateString()
导致查询查找的是
字符串
值,而不是
日期
。有两种方法可以解决这个问题:

(...)->where('created_at', '>=', Carbon::now()->startOfMonth())
// OR
(...)->whereDate('created_at', '>=', Carbon::now()->startOfMonth()->toDateString())

如果只使用
Carbon
变量,Laravel知道检查
date
逻辑。或者,如果您使用的是
字符串
,则可以通过使用
->whereDate()

覆盖
where
逻辑,将其视为
日期
,您可能需要查看
->whereDate