Laravel 检查今天的日期是否存在任何记录

时间:2021-05-10 21:31:14

标签: laravel

我有一个约会类,在刀片模板中,我试图检查用户是否有今天的约会。有一个单独的“日期”列,格式为 Y-m-d。

我曾尝试使用 Auth::user()->appointments->pluck('date') === Carbon\Carbon::now()->format('Y-m-d'),但这不起作用,因为 pluck('date') 返回 'Y-m-d H:m:s'。

我也在尝试 Auth::user()->appointments->where('date', Carbon\Carbon::now()->tz(Auth::user()->timezone))->count(),但没有成功。

感谢任何帮助。谢谢!

2 个答案:

答案 0 :(得分:1)

在这种情况下,laravel 中有一个名为 whereDate 的方法会更好

Auth::user()->appointments->whereDate('date', Carbon\Carbon::now()->tz(Auth::user()->timezone))->count();

欲了解更多信息,请查看 Additional Where Clauses

答案 1 :(得分:1)

您可以使用 laravel eloquent whereDate 来获取今天创建的记录。

例如:$query->whereDate('created_at', today())

你可以做类似的事情

Appointments::whereDate('date', today((auth()->user()->timezone)))
->where('user_id', auth()->user()->id)
->count();

Auth::user()->appointments->whereDate('date', today(Auth::user()>timezone)))>count();
相关问题