Playing
07. Passing a Builder around

Transcript

00:00
Another reason that you might want to access the builder directly is if you need to pass this builder into another class or somewhere else and this is going to be a slightly longer episode because we're going to take a look and a practical example of building out a Filtering system from our query string. So let's say that we had a list of posts like we've been looking at
00:20
We might want to have some sort of published true We might want to have some sort of date or order in here and we would handle this with a filter a class and then a list of Filter classes that we can enable and disable if we want to so let's look at how we would write this out
00:38
First of all, and then we'll see that builder going into this and we'll pull together the full example So you could use this in your own projects if you wanted to Okay, so I want to create maybe some sort of new filter in here. What would that take in? Well, it would probably take the entire request object in so we can pluck out the query string items
00:56
and then we'd want to pass in a builder because we want a fresh post builder that we can start to Iterate through all of the filters that we want to apply and then return the final result so in this case the query method comes in really handy because we can just pass the post builder directly into this and Just start to use it and then what we can do is maybe call a filter method on here
01:17
Let's just pull that down to the next line and we can a pass an array in of filters that we want to apply So for example a published filter, we'll just take a look at this as an example for now not add any others But by the end of this you'll be able to pass in any other filters you want to so we don't have these classes in Here at the moment. So let's just go ahead and create a filtering
01:38
folder here and let's create a filter and let's create out a Published Filter we'll make this a little bit messy. It could do with a much better organization, but we'll keep it as it is for now so app and filtering and
01:56
this will be our filter and Let's just copy and paste this to published filter Like so, okay, so the filter here we know needs to take in to the constructor the request object So let's pull that in and also a builder so we know that we are actually getting a builder in here now
02:17
And let's just make these protected at this level so we can skip having to assign them and let's make sure we pull in the namespace for both of these and Then we can look at pulling this together
02:30
Okay, so we have got our constructor with the two things that we need passed in Remember if we head over to roots and web We've got this filter method that we need to pass in so let's go ahead and create out a filter method in here that's going to take in an array of
02:47
Filters and then what we can do is we can iterate through these Apply each filter that we've got passed in to this builder So we can continue to chain and then just return the result back to us. Okay, let's take a look So the filters that we pass in could do with a common interface
03:07
We won't do that now because we're just looking an example But let's just start to iterate through each of these filters as a filter and then in here Let's go and check if the request has a parameter on this filter object So we can say if it doesn't then we want to do an early return
03:25
So this request I'm gonna say has and on our filter object We'll have some sort of query parameter that we can define. We'll get to that in just a second now if that doesn't exist There's no point in filtering because basically this means it doesn't exist in the query string Otherwise with each individual filter that we pass in we're going to go ahead and apply that filter
03:45
We're going to pass the builder through at each stage that will build this query up and then we'll pass in this request get and we'll grab The filter that we get from that or the query string part from that and then finally at the end of this We'll go ahead and return the builder
04:02
So we can continue to do what we want with it We'll go over this in just a minute when we've done the full implementation just in case it doesn't make any sense But let's fill in this published filter now Like I said, this could do with a common interface so we know that it always has the apply method
04:16
Let's go ahead and just apply this now though We're gonna get into this a builder Let's do that in the right place And it will get a value in so that might be published true or false, for example
04:28
So let's pull that builder into here and then let's just do what we need to do So builder where published and we'll pass in the value now Remember if we come over to our filter, we're using this query param property on here. So let's create this out now Query param and let's decide what we want this to be called in our query string and we'll just call that published
04:50
Okay, let's go over this really quickly before we start to run it So in our implementation here, let's just pull in filter and published filter We're passing through the request and a builder so we can chain Anything that we do on our filters to this builder if we come over to our filter
05:07
for each of our filters Which we've just got one at the moment if it doesn't have if the request doesn't have the parameter that exists here so if we don't have Published in the query string then there's no point continuing
05:19
So what we do is we just do a return and skip over that and that should actually be continue And then otherwise we call that apply method Passing through the builder so we can perform some sort of action on that With the value from the query string once all of our filters have been applied
05:37
We then return the builder so we can chain on and do something like get or whatever we need to do So let's try this out. Let's go over and just give this a refresh and we're not actually doing anything with the result yet so it would help if we die dumped on posts and yet and
05:54
Gave that a refresh we get to now we can start to scope by published So I'm going to say publish true. Of course, we get one here Which is published and if I say published false, for example We're gonna end up of course with the post that has not been published
06:13
That's just a really really basic implementation of some sort of filter of course what you could now do is apply more filters to this if you wanted to so you could apply some sort of order filter if You wanted to it's entirely up to you
06:28
We do have another course on this as well if you want to learn how to do this But make it a little bit better So there we go passing the builder in its sort of raw instance Directly to something can be really really helpful. And in this case, I think this makes sense
7 episodes 23 mins

Overview

Ever used the Eloquent query() method in Laravel before? In this short course, we'll dive into what this method does, and demonstrate the ways it can be used.

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

Episode discussion

No comments, yet. Be the first!