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
02. How pipelines work

Transcript

00:00
Let's dedicate this episode just to look at pipelines without our query filters just so we can figure out what's going on. So I've already gone ahead and created a forum index controller here. It's just an invocable controller with
00:13
nothing in it at the moment and I have this open in the browser. So what we're going to do is start to use Laravel's pipeline to pipe a single number through this and then perform multiple operations on that number and that's exactly what we're going to be doing with our query filters. We're going to be
00:30
passing the builder through and then doing lots of different things e.g. applying lots of different filters. Now at the time of recording pipeline is an undocumented feature within Laravel but that doesn't mean it's not used a lot within the framework. It's actually used for middleware and as we start to go
00:47
through this example just have in the back of your mind middleware and how middleware works. We'll come back to this in just a second. So what we're going to do is start off by assigning something to a variable just called number. What we're going to do is from our container within Laravel we're going to go ahead
01:05
and grab the pipeline instance. Now to pull the namespace in for this it's illuminate and pipeline and now we have a fresh pipeline class in here or instance in here. So let's go ahead and just die dump on number. Let's head over to the browser and give this a refresh. So you can see that we've got this
01:21
pipeline class in here. Now notice within this we've got multiple pipes. So they are what we pipe the data that we need to through. So to do this we go ahead and use send and in our case we're just going to send a single number through. Let's just choose five and we're going to pipe that specific variable. It doesn't
01:40
matter what that is. It could be a number, it could be an object, it could be a string. We're going to go ahead and pipe that through multiple pipes that we've just seen over here. So what do we want to do in here? Well we would usually pass a class through just to keep things tidier but we can also pass this through
01:58
just closures. So in this case we're going to go ahead and create our closure. We will convert this over to a class in a minute so we can see what we're doing and into this we get first of all the thing that we're piping through. So that's going to be the number itself. The second thing we get is next. Now I mentioned middleware
02:16
earlier and of course when we work with middleware in Laravel, if we just take a really quick look under our middleware section, let's just open up say verify cross-site request forgery token. Probably a bad example because we don't have anything in there. Let's go redirect if authenticated and you can see here
02:33
that when we handle this we get the request passed through and we get next and that's how we call the next piece of middleware passing in the current state of the request. So this works in pretty much the same way because Laravel middleware uses pipelines. So let's not do anything to the number. Let's just go
02:51
ahead and return the next pipe in this passing through the current state of whatever we're sending through here which in this case is the number. So just note really carefully that this is an array of closures or it could be an array of classes. Now to finish this up we're going to go ahead and call
03:09
then return. That's going to return us the value that we passed in that's been modified or not modified by each of these pipes. So when we go ahead and die dump on number we're actually going to get back five which has been passed through each of the pipes we define in here and you can see sure enough we get
03:28
five dumped out in here. Now within each of these pipes, in this case they're just closures at the moment, we can go ahead and modify this number. So let's reassign this to add one onto it. So this is going to be passed through this pipe. It's going to return the value and of course because we're passing the
03:45
number that we've modified into the next callable thing then that's going to eventually return the number six as you can see. So what we could do is we could create another closure in here or another pipe and maybe we can multiply this by two instead. So what's going to happen is it's going to run through the
04:04
first pipe and it's going to add one to it which of course gives us six and then the next pipe six is going to be passed into here and that's going to be multiplied by two which of course gives us 12. So if we head over and give that refresh sure enough we get the value 12 and we could just keep going with these.
04:20
We can add as many pipes as we want. Of course there's no limit here and then we return the final result. Now let's get this second one because hopefully the concept of multiple pipes makes sense and let's create out a class for this instead. So I'm just going to dump this in the main app directory but of course
04:37
we would want to organize this and we will be doing that when we get to query filters. So let's just call this add number and we'll go ahead and just define this out. The namespace here is app and the class here is add number. Now how do we define this because at the moment we're just using a closure. What we do
04:54
is we go ahead and create out a handle method in here which will be called by the pipeline class. So let's go ahead and take the same signature to this method, pass it into here and do exactly the same thing and then we can get rid of this closure and instead we can go ahead and call add number in here. Now we
05:14
give the fully qualified class name in here so that can be called and then we just go ahead and paste in what we had in before. So that's pretty much what we need to do. Let's just make sure that we pull in the namespace for add number. Let's go over and as you can see we get six. So what we could do is create
05:29
another class if we wanted to, to multiply this by two if you wanted to add that in there but hopefully you kind of get the idea. So you can have a closure based in here or as we've seen here we can pass in a class with a handle method, exactly the same signature, making sure we call next and
05:48
we are good to go. So that's pretty much how pipelines work. There are other methods on here but for the purpose of this course these are the only things that we're going to be using. We send a particular value through multiple pipelines and then return that modified value.
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!