Playing
01. Introduction and demo

Transcript

00:00
Implementing query filters in your projects can get really messy really quickly.
00:05
In this course, I'm going to show you a method of filtering and ordering data using pipelines, which is an undocumented native Laravel feature. First of all, let's take a look at a working example and then we'll dive into a sneak peek of the code.
00:22
So let's imagine as you can see by this example, we're building out a forum which lists a bunch of discussions. I've only got two here. One of these is marked as solved within the database and one is not.
00:35
And of course, as you'd imagine with a Laravel app, both of these have a created at date, which we can order by. So let's go ahead and start to introduce our first query filter. The first one I'm going to choose here, which you can see is already in my browser history,
00:51
is unsolved true. So what this filter is doing is going ahead and picking out only the discussions which are not marked as solved. And we can see this over in the database just here with this solved at column, which is
01:05
a date time column. Now what we can also do is go ahead and order these. So let's go ahead and say created at order in ascending order. And of course, we can switch this to descending order as well.
01:17
And that will go ahead and change the order of what we see. Now these are two very, very simple examples. But of course, whatever you're building, you can take exactly what we're going to implement here and apply it to as many filters as you need.
01:31
And you can go ahead and combine these as well. So we could also say and unsolved true. And of course, we just see this one in here. But if, for example, in the database, this wasn't solved, let's go ahead and hit save
01:44
changes, come over and give that a refresh. We can order these in ascending order as well. So how does this work? Well, the first thing to know is if we come over to the forum index controller, you can
01:56
see that here I've got this method called query filters. Each of the filters that we're going to create is a class of its own. So what we're not going to be doing is creating a bunch of if statements. Every single filter lives inside of its own class.
02:12
What we're then going to do is pass the overall builder that we start with through a Laravel pipeline, calling each of these query filters to check if they exist and really importantly, what data they contain, and then go ahead and filter by them. So regardless of how many filters you have, this leaves you with a really, really neat
02:30
solution for filtering. OK, so if you've not come across pipelines in Laravel before, let's head over to the next episode and take a look at them really quickly before we start to implement what we've seen here.
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!