我有一个约会类,在刀片模板中,我试图检查用户是否有今天的约会。有一个单独的“日期”列,格式为 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()
,但没有成功。
感谢任何帮助。谢谢!
答案 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();