So how do we reorder something that's already been ordered in Eloquent? Well, let's take a look at this example application that I've set up here. We just have this really simple discussion index controller,
00:11
which from a discussion that we get, grabs a list of posts and then goes ahead and just pass them through to this view. If we open this view up, that's just iterating through each of these posts and outputting the body.
00:25
You can see that just over here in the browser. At the moment, they're not really ordered correctly. We haven't specified the ordering anywhere. If we take a look in the database, you can see that the first post was posted on the 25th.
00:38
The post in between, which I've named just so we can see which ones we have here, was the 27th and then the 30th. So this one should be at the top, then this one, and then lastly, we should see the first post. That's not the case at the moment.
00:53
So how might we order these? Well, typically what I would do is over in the discussion model, where we have this post relationship, I would just add on the latest scope in here. That's just because most of the time when we're accessing the posts for a discussion
01:08
or any of the data related to the models that you're creating, we would always want the latest ones at the top. You might not want to do this, but I find it always helps just so we always get that data in its default form as we want it.
01:22
So now that we've done that and made that change, sure enough, the latest post is at the top, then we have this in between one, and then the first one is at the very bottom. The problem now is what happens if we wanted to add some sort of sorting to the URL?
01:36
For example, we might want to say order by descending or ascending. It doesn't really matter how you want to reorder these. But the problem that we're going to see here is that we can't just go ahead and reorder them because we've already defined the order over on the post relationship here.
01:52
So if we go over to this discussion controller, let's take a look at how we might do this inside of here. Now, we're going to have some sort of logic here. It might not be an if statement.
02:01
I'm just going to add if true to simulate some sort of logic that would reorder these things. And let's go ahead and reassign posts to discussion posts. And maybe we wanted to say oldest here. So let's say that we wanted to reorder these to grab the oldest instead of the latest.
02:21
So looking at the logic here, you would think that this would work. So if some kind of condition is true, let's go ahead and regrab the posts out, but order these by the oldest first. So if we come over and give this a refresh, you can see that this isn't working.
02:35
Nothing has changed here in the order. Now, the reason for this is the order by has already been applied to the query. We can't go ahead and add another one. And Laravel by default won't go ahead and replace this.
02:49
What we need to do instead is reorder these. And there's a couple of different ways that we can do this. Now, when you're figuring out these kind of things, the best thing to do is always to dive into the source code.
02:59
So we're going to head over to the query builder. And you can see here that we've got this reorder method. So we can specify a column here, which by default is null. And we can just specify a direction, which by default is ascending.
03:14
Now, we don't actually need to give the column in here. There's two different ways that we can use this. So if we don't give a column, what we can do is just say reorder. And then we can continue to chain on any things that we want to use to order it.
03:28
For example, now we're going to say we want the oldest at the top. So just by applying that reorder method, that's going to go ahead and clear that out ready for the new ordering to come in. If we give this a refresh now, you can see now the first post is at the top.
03:40
We still have that in between one. And then we have the latest post at the bottom. So you could do it like this, and this would work really well if you had some sort of scope in here where you didn't want to specify the columns directly
03:52
within a controller or wherever you're doing this. Now, the second way to do this, if you didn't want to use something like oldest and you just wanted to specify the column, you could go ahead and do that by passing in the column as we've already seen over in the builder just here
04:06
in the method signature, and then go ahead and specify the direction. So in this case, we're going to say created at in descending order. Or if we wanted to specify ascending, then of course, that would change that around as well.
04:19
So that's pretty much it. Of course, what you're doing in your own applications is going to be wildly different to what I have done here. But if you ever find yourself having some sort of default order on something,
04:30
whether that's from your own code or from a third-party package, and you need to reorder it, you now know that you can use the reorder method either on its own and then order again afterwards like this, or you can go ahead and specify the column and the direction.
1 episode• 4 mins•2 years ago
Overview
If you've already applied ordering to an Eloquent query, you'll have to 'reset' it if you need to change the order later on. Here's how!