07. Passing a Builder around


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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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


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!


No comments, yet. Be the first to leave a comment.