Laravel Check if Any Records Exist with Today's Date

Viewed 936

I have an Appointment class and in a blade template I am trying to check if the user has any appointments with today's date. There is a separate "date" column where the format is Y-m-d.

I have tried using Auth::user()->appointments->pluck('date') === Carbon\Carbon::now()->format('Y-m-d'), but that doesn't work because pluck('date') returns 'Y-m-d H:m:s'.

I'm also trying Auth::user()->appointments->where('date', Carbon\Carbon::now()->tz(Auth::user()->timezone))->count(), but no luck.

Any help is appreciated. Thank you!

2 Answers

You can use laravel eloquent whereDate to get the record created today.

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

you can do something like

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

or

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

there is a method called whereDate in laravel would be better in this case

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

for more info check Additional Where Clauses

Related