In this episode, we dive into a super helpful new feature added to Laravel Eloquent: relative date helpers. If you've done much querying with dates in Laravel, you know things can get a little messy, especially when you need to compare dates or filter models by when something is due.
We start off by looking at the usual way of handling these queries—using Carbon and doing manual date comparisons in your Eloquent queries. Then, I show you how these new helpers can replace all that boilerplate with much cleaner, more readable code. For example, instead of writing your own end-of-day logic, you can now just do whereAfterToday('column')
, and it works right out of the box.
We walk through each of the main helpers, like wherePast
, whereFuture
, whereNowOrPast
, whereNowOrFuture
, whereToday
, whereBeforeToday
, and whereAfterToday
. I explain how each one works, when to use them, and the subtle differences you should watch for when doing stuff like including the current moment or not.
To make things even more flexible, I also show how you can chain these helpers together with other qualifiers like or
, so you can tackle more complex date logic without losing readability or getting lost in parentheses.
All the code examples and a summary of the new methods are also listed in the GitHub repo for easy reference. After watching this video, you'll be able to write much cleaner and more expressive date queries in your Laravel projects!