This episode is for members only

Sign up to access "Easy Query Filters with Laravel Pipelines" right now.

Get started
Already a member? Sign in to continue
Playing
04. Creating an ordering filter

Transcript

00:00
just to drill this in let's go ahead and create another filter that we saw from the introduction and that is to order these in a specific order so we've already got this in the browser history here we want to be able to switch between ascending and descending order for this created at column now another downside to this approach is it's not very dynamic so for example adding in some sort of
00:20
order would be a little bit more tricky you'd have to implement more logic in here and it would probably be better to reach for a package that has already solved this but in this case i'm just going to look for a created at order query value and take that value and order the results by that particular order now we have to think about this really carefully because we need to examine the
00:43
first thing that we're passing in we're using discussion latest what we could do is swap this out for discussion query but ideally the first thing that we pass through without any of the filters needs to be a kind of sensible default in this case ordering these with the latest at the top kind of makes sense now i'm just going to make a modification in here so discussion two i'm going
01:04
to go ahead and just manually set this at the moment these are both identical and that's of course going to bump the discussion two to the top because it's created latest okay so let's create another query filter in here i'm just going to go ahead and copy and paste this one over to this and let's go ahead and call this created at order query filter we'll go ahead and paste this in
01:28
let's change the name created at order query filter and let's go ahead and just get rid of all of this just so we can kind of start afresh so let's go over to our forum index controller we'll make sure we go ahead and pull this in so created at order query filter and we're done so there's no change at the moment except the fact that we're not passing the next builder
01:56
through to this so obviously that's the first thing that we would want to do in here even if we're not filtering anything so that'll go ahead and get rid of that error the reason that that happens is it's trying to call get at this point on a null value that's been returned because we're not explicitly calling the next pipe okay so how would a query filter like this look well again
02:19
we're going to use a defensive approach here and we're going to go ahead and check if something is not there which is going to be the direction that we want to order this in which we know we can be ascending or descending so let's go ahead and say request get and just use that thing that we've already used created at order that's going to return to us the actual direction that we want to
02:40
use ascending or descending so if that's not the case and it doesn't exist of course we want to go ahead and return call the next pipe passing that builder through to the next pipe otherwise we want to go ahead and start to reorder this now because over in our forum index controller we've already used latest going ahead and ordering this again is not going to work so if we use order by created at
03:06
in the direction that we've defined this isn't actually going to work so we can say ascending just doesn't work the reason that this doesn't work is we need to reorder the results before we go ahead and order it again let's try this out give that a refresh and there we go so every single filter that you create now is pretty much going to follow this same pattern of course if
03:28
you have more advanced filters you're going to end up with a lot more code in here which is why it's usually better if you have much more complex requirements to go ahead and reach for a package where the work's already been done for you but for very simple things this works really nicely okay we're just going to finish up by going ahead and looking at paginating really quickly
03:48
so i'm going to go ahead and paginate this by one per page just for the fact that we've only got two results we also don't have any kind of styling here at all as well so when we do implement the pagination it's going to look a little bit funny but the concept remains the same let's go over to index here and go ahead and output the links for our discussions so in here we just need to say
04:10
discussions and links and that will give us the pagination in here of course we have a huge svg image there which we can ignore so we've got showing one to one of two results hitting next works in exactly the same way so we can just continue to use anything that we would normally use within our eloquent builder paginate get whatever we need to do now i'm going to switch
04:33
this back to get just so we end up with a nice clean page here and of course we're going to get rid of them links but that's something you can do if you need to okay so we're going to head over to the next episode and look at passing additional arguments through to each of our filters to improve the testability
6 episodes 35 mins

Overview

Applying result filtering with query strings can mess up your controllers quickly. Let's reach for a solution using Pipelines, an undocumented but seriously powerful feature of Laravel.

Every filter (e.g. only show activate users) will have its own class, neatly tucked away. We'll also cover testing, and see how this method makes isolated testing much easier.

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

Episode discussion

No comments, yet. Be the first!