Courses

Laravel 11 Eloquent: Expert Level

whereDate() and other whereX Methods

Summary of this lesson:
- Using whereDate() and related date query methods
- Understanding different date filtering approaches
- Working with DB::raw() for date queries
- Best practices for column-specific where clauses

What if you have a timestamp or datetime column and want to query by date? How do you filter that? There are a few ways to use raw queries, but I will show you a few helpful methods in Laravel.


Example 1: DB::raw()

Because created_at is a datetime column, the SQL date function can be used.

User::where(\DB::raw('DATE('created_at)'), '2024-03-01')->first();

And, of course, it returns the user registered on that date.


Example 2: whereDate()

Instead of doing DB::raw() and using SQL functions, you can use the whereDate() Eloquent method.

User::whereDate('created_at', '2024-03-01')->first();

Also, instead of date, you can check...

The full lesson is only for Premium Members.
Want to access all 28 lessons of this course? (71 min read)

You also get:

  • 69 courses (majority in latest Laravel 11)
  • Premium tutorials
  • Access to repositories
  • Private Discord