Grouping Feed by Date0:00
If you take a look at your LaraCast profile page, and you scroll down to your Feeds section, you'll see we have a list of the most recent episodes grouped according to how long ago they were released. So, for example, any videos released today go up here, next, any videos released in the current week, next, videos from the previous week, and then finally, much older videos. Now, luckily, if we work with Laravel Collections and Carbon, this ends up being laughably simple. So let's review an example. I'm going to boot up php artisan tinker here, and I've created a model called Video. So if we get those in descending order, like so, you'll see we have about 50 here, it looks like, where the most recent one was created today, and then we have some created a few
So if we get those in descending order, like so, you'll see we have about 50 here, it looks like, where the most recent one was created today, and then we have some created a few days ago, all the way back to a number of months ago. Alright, so let's see if we can group these videos in the same way that we've done so on the profile page. So I will switch over to Sublime, and let's begin, we'll just return directly here. So we're going to grab our video, I'll import that, we want them in descending order by the created_at timestamp, and then we'll get the results. Okay, let's start with this and then work our way up. Now if I load this in Firefox, there's no grouping, we just have a big list, a big collection.
SQL vs PHP Grouping1:11
Okay, let's start with this and then work our way up. Now if I load this in Firefox, there's no grouping, we just have a big list, a big collection of videos in descending order. Okay, now when it comes to grouping, you can of course do this in two ways. One, you can group it on the SQL query end, or two, you can group it on the php end. And really, there's no right or wrong, you'll end up doing both. But I find when the grouping gets a little more tricky, a little more complicated, I instead prefer to do that on the php end. So think about it, after we run this query, we receive a collection. Take a look at that here, and of course you have access to countless methods here.
Using Collection groupBy1:42
So think about it, after we run this query, we receive a collection. Take a look at that here, and of course you have access to countless methods here. What we want is the groupBy method. So let's get started. We're going to group all of these videos according to some kind of logic we provide. Now as a first illustration, maybe you want to split them by even and odd IDs. Okay, so you could say, look at the video ID, mod two, and if that equals zero, then include this specific video in that group. If it doesn't return zero, return it in the other group. Okay, so now if I come back and give this a refresh, you'll see I have two new collections.
If it doesn't return zero, return it in the other group. Okay, so now if I come back and give this a refresh, you'll see I have two new collections. This first one has only the videos with odd numbers for the ID, one, three, five, seven, nine. And of course, the second one will have all of the even IDs, two, four, six, eight. And when you think about it, this gives you a lot of power, really. So for example, here's another illustration. Maybe we want one group or one collection for all the videos released in the latest month, and then another group or another collection for all of the other videos. Okay, well, let's look at the createdAtTimestamp.
month, and then another group or another collection for all of the other videos. Okay, well, let's look at the createdAtTimeStamp. And there's all sorts of methods we can call, like isCurrentWeek, isCurrentMonth. Let's take a look at that one. All right, now if I come back and give that one a refresh, let's see what we have here. This first one, let's see, okay, it has all the videos that were not released in the current month. So as I record this, we are in October, so this will have just about everything else. Okay, but if we go to the second collection, this will exclusively contain videos that were created in the current month, which again is October.
Implementing Today/Week Buckets4:06
We have a section for today, this week, last week, and older. All right, let's get started. Well, if the video was created today, then we'll say today. Next, if the video was created in the current week, we can say is current week. And of course, you can review the documentation for this. Let's see, is current week. Yeah, here's all the various methods you can call. And you can also see, we will want last week as well. This week, we'll do another one. If it was the last week, we can say last week.
Extracting to Model Method5:01
Here are the other videos that were released this week. Here are the videos from the previous week. And then finally, here's everything else. Okay, so now, at this point, you only need to iterate over this array and echo the contents for each video. And that's it. So now, the final step, if you want to keep it like this, I don't see a problem, or you could clean things up by adding a custom method to your Video class. So for example, maybe you have a static method, and maybe recentByDate, something like that. And then you would grab this query, like so.
Creating a Custom Collection5:26
So for example, maybe you have a static method, and maybe recentByDate, something like that. And then you would grab this query, like so. And then finally, we'll just say, self::latest(), return that, clean it up, and we end up with something like that. All right, so now, I can say return video::recentByDate(), and this will still give us the exact same thing. Now a second approach that could also be useful is to create a custom collection. So let's review that, and then we'll call it a day. We'll call this VideoCollection, and then we can't forget to have this extend Laravel\Collections\Collection.
We'll call this video Collection, and then we can't forget to have this extend Laravel's Collection class. Now we have a dedicated class for any special grouping, group by date or recent, whatever you want. Okay, so now, with this approach, once again, let's grab all of this. I'll paste it in here. But now, we don't need to perform a query, we'll assume we already have one. So I'll just say, return this groupBy, like so. So now, back to our model, I can get rid of that. And then in the routes file, we would perform our query again.
So now, back to our model, I can get rid of that. And then in the routes file, we would perform our query again. But now, at this point, once we're finished, we will have an instance of VideoCollection. So I could say, groupBy date, or recent, might be a better name. Okay, just one last step. Now if I come back and give this a refresh, it is going to fail, and that's because at the moment, we're not using that custom collection. So that's the final step. We'll go back to our Video model, and be explicit about which kind of collection we want to return.
We'll go back to our Video model, and be explicit about which kind of collection we want to return. So basically, we have to override this new collection method that you'll see right here. So I can copy that, and paste it in. And now I can just say, okay, well when you build up a new collection, return to me a new instance of VideoCollection. And that's all there is to it. So now, if we come back and refresh, once again, we get exactly what we had before. But yeah, in certain situations, this will be a cleaner approach. Because think about it, now, especially if you have a site dedicated to videos, like
But yeah, in certain situations, this will be a cleaner approach. Because think about it, now, especially if you have a site dedicated to videos, like Lericast does, for anything collection related, you now have a dedicated spot to put that logic. So now, back to our routes file, this is what we end up with.
