Playing
21. Eloquent Relative Date Helpers

Transcript

00:00
If you've been working with Laravel for any length of time, you'll know that Eloquent provides us a bunch of helpers to help us write more fluent queries.
00:09
Now, what's been added recently to Laravel is a bunch of relative date helpers. Now, when we work with dates, particularly with the database, things tend to start to get a little bit more complex. We have to do comparisons and build up and manipulate dates to create a comparison, perhaps with a carbon object. I'm going to show you an example of that first with some of the data that we've already got here.
00:35
And then I'm going to show you these new helpers. And we're pretty much just going to swap over from what we had to write before to what we can now write. I'll go through each of the new helpers that have been added very briefly. And then, of course, you can refer back to these now that they are tucked away in the back of your brain. So let's go over to the database and just take a look at the data that we're working with here.
00:55
So ignore this immediate column, because we're going to be looking at a slight variation of these later with another column. But we've got a due date now. Today's date is February the 12th, 2025. And you can see I've got a mixture here. Some are in the past, some are today and some are in the future. So we can use them to determine if the methods that we're going to use are plucking the data out correctly.
01:21
OK, so the first example that we're going to look at is, let's say that we want to grab all invoices where the due date is greater than today. So basically the end of today onwards, I want to know what's due. So let's modify this query that I've got here at the moment, which at the moment we're just dumping out the collection here. And let's go ahead and take a look. So what I'm actually going to do, just so you can see this data a little bit more easily,
01:49
I'm going to go ahead and pluck out the due date and I'm going to just cast this to an array. So let's grab this data first. Let's go ahead and pluck this, cast it to an array. And basically then we're just going to be able to see all of the dates. So that's just everything right now. OK, so in between this, how how would we do this before? Well, we would just use a normal where clause here.
02:12
And we would say due at. And what we can do is still continue to use a comparison operator here. So I can say greater than or I can say greater than or equal to. It doesn't really matter. I'm going to say greater than or equal to now. So I'm going to use the now helper in Laravel to give me back a carbon object. And then I'm going to say end of day. So that's just a method that we can use on carbon to manipulate the date to the end of the day.
02:39
When we go ahead and pass this into the query builder, this will get cast to a string and that will get compared in the database. So everything that's due after the end of today. So that does not include today. Let's go ahead and give this a refresh and see what we get. And yet, sure enough, because it's February, we've just got the two that are in March. So the two that are due here in March. So you kind of get the idea.
03:00
And if you've been working with Laravel for any length of time with any kind of dates, you've probably written something like this already. Let's go through some of the methods that have been added, the relative date helpers that have been added. And we'll start with a direct replacement of what we have just done. So how do we do this? Well, we're going to say where after today and then we're going to give the column name.
03:26
So much better than having to do this manually. Let's go over to the browser and refresh. And of course, we get exactly the same result because it's doing exactly the same thing behind the scenes. OK, so now that we've seen what these are, let's just go through each one really, really quickly. I'll leave a list of these and exactly what they do in the GitHub repository for this course.
03:49
So you can go ahead and check them out. Let's go through these really quickly. So we've got these are kind of grouped together. So the first ones that we've got are where past and where future. So where past will give you where anything is, obviously, in the past. And then we have where future, which, of course, is going to be into the future.
04:09
Now, the tricky thing about these and what initially confused me is where past and where future will take into account after the current date and time. What we also have, aside from where past and where future, is where now or past. So that will be the exact date and time that we're in or in the past. And we have where now or future. So like everything, these are just depend on the kind of comparisons that you're doing.
04:41
And whether you want to say, let's say it's 10 o'clock in the morning. Do you want to include that 10 o'clock or do you just want to include 10.01 onwards? So hopefully that makes sense. So we've seen a couple of them. We also have where today, let's say where today, that will just give anything that is within the current day, which we just have one record for here in the database.
05:05
And then we have where before today or where after today. So where before today, that will just basically take the start of the current day and anything before that, pretty much like we just did with that query. And the same thing where after today, that will look at the end of the day and include anything after the end of today as well. And you can see we get the results that we saw earlier.
05:30
OK, so one more thing here that I want to cover. Let's just choose any of these. We'll say where after today. What we can also do is couple these and use an OR modifier. So, for example, let's say that we wanted to find any invoices that required an immediate payment. So this is just a Boolean set to true or false or had some sort of condition around the due date.
05:55
Well, in this case, what we would do is we would say something like where and immediate true. Now, this is where immediate true and where after today. We can also modify any of the methods that we're looking at here with OR. So we can just say where immediate is true or where it's due after today.
06:18
If we give that a refresh, we get both of them records because that both comes into scope with the data that we're working with. So as well as these code examples, I'll go ahead and list these out in the GitHub repository so you can have a good look through them. But now that we've covered these, you can pretty much just refer back to the documentation whenever you get to the point where you might be working with dates and might need these shortcuts. So, again, Eloquent is getting better. Really, really handy to have these methods that we can just lean on rather than having to manipulate the dates ourselves and do a strict comparison.
26 episodes2 hrs 34 mins

Overview

Need to know what’s new in Laravel as it happens? Every episode of this course is dedicated to covering the most interesting and useful Laravel additions in detail, so you’re ready to start using them in your applications.

Check back often, and stay completely up-to-date with Laravel.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!