This episode is for members only

Sign up to access "Custom Laravel Query Builders" right now.

Get started
Already a member? Sign in to continue
Playing
04. A multiple query builder example

Transcript

00:00
So to wrap things up let's go ahead and create out a related model to this user like an address, create a custom query builder for that address so we can have a kind of scope on that and then
00:12
let's implement another method on our query builder for our user and introduce that additional model. Now I realize at the moment that might not make much sense but I'm just trying to give you a sense of how this can tie into your other models in more complex applications. So let's start out with a newer model so let's go ahead and make out an address model in here,
00:36
we'll create a migration and a factory in there as well so we can generate out some records and let's go ahead and create out the migration for the create addresses table. So this needs to relate to a user so we'll just use a foreign id in here for user id and we'll go ahead and constrain this and then let's just include maybe a boolean in here to see say if it's verified or not
00:58
some kind of data like that and we'll set a default here of one or true we don't necessarily need to include any other information in here for our testing but of course you could go ahead and fill in this with any of the details that you need. So let's go ahead and just migrate these changes we'll hop straight over to the user model and we'll go ahead and create a relationship here
01:19
so let's create out the user has many addresses let's return this has many and we'll go ahead and pop that address fully qualified namespace in there. Okay so our users can now have multiple addresses let's go over to tinker again and just create out some fake addresses so we're going to say address factory and let's create three of them we're using times and we'll create these
01:47
under the user id of let's just say one and then we'll switch it over in the database so each user has one address so that'll be one two and three there we go now the goal that we're trying to reach here is that we might want to within code if we just hop back over to our web routes we might want to say well we want to grab all users that have a verified email address and have a
02:13
verified address as well so hopefully that's not too complicated we basically want to chain these two things on but make use of that additional model so let's go ahead and get rid of that for now and let's set let's go over to users we've got these two who are not verified three is verified so let's make three have a verified address and let's make one and two not have a verified address
02:38
so if we kind of keep our query as it is but then say has verified address we should just get back user three and of course this is just an example but you can apply this to more useful queries okay so now what we need to do is just run through what we did with our user model but for our address model so let's go ahead and just copy and paste everything that we did here in terms of this
03:02
query and new eloquent method and we can just switch this out to address builder which we haven't created yet but we can fill this in and then go ahead and create it okay so time to create this builder let's go ahead and say address builder and again we could just copy over the user builder just so we have a little bit of structure here of course change the name to
03:22
address builder and here we don't want this has verified email we want to introduce a new sort of query into here so let's just keep this nice and short of course the naming is up to you but i'm just going to say verified in this case because when we call this specifically on an address address and then chaining on verified kind of makes sense so what i didn't mention when we
03:44
spoke about type hinting before is it might be useful to type in this as static because when we return this we know we need to return an instance of the current builder so that just helps you kind of work out that you do need to say well i need to return this and then where verified is true for example if we can spell verified correctly so that's just a method on
04:07
our address so what we can now do is over in our web roots let's just test this out really quickly by commenting this out we can use this alone on the address model itself so let's say address query and verified again that gets filled in for us nicely if we actually if we come over to our address model make sure we pull this namespace in there we go so what we'll do here is just pull
04:33
this name pull this model in at the top so we get our auto completion we're going to go ahead and say query verify we know that's a method which again because we've implemented that builder gets put in there as a method for us and then we can just say get and we can die dump and that should be addresses but we'll just leave the naming as it is and there we go sure enough we
04:53
just get the one email address or one physical address that has been verified okay so now what we want to do this is where it gets interesting we want to return users where maybe they verified their email but also have a verified address as we spoke about so where does this part of our query builder go well it goes inside of the user builder because that's what we're trying to do we're trying
05:17
to get users who have verified their address so let's look at a really simple example of how we might do this without relating the other address builder into this so to do this we would go ahead and create our method first of all and let's say has verified address so that's their physical address and remember in here we can go ahead and return static if we want to we can do that for
05:43
this one as well if we want to so in here we're going to return this and we're going to use something like where has and we'll give the relationship in here which is the addresses relationship we then get a closure in here and we get a query builder inside of here now this query builder here is actually going to be an address builder we'll talk about that in
06:05
just a second but if we were just doing this normally what we would do is we would just say query where verified and true so we're basically diving into the addresses relationship and we're getting back a user where they only have addresses that have been verified so let's go ahead and take this method name and we'll use this over on here like so and let's go
06:28
over and give that a refresh now at the moment it doesn't look like we're getting anything just because we're not dying and dumping but here we go we get one user back which is that user with an id of three because they have a verified email address but they also have a verified address as well of course we can play around with our data and we could fill this in just to prove that this
06:47
is actually working so let's go ahead and set this to true as well and let's go over and check this out and of course we get two users in here so what we can actually do is we can refactor this down to make use of the kind of scopes not scopes methods on our query builder that we've used within our address builder so we know that in our address builder we've got this verified method which we've
07:10
already seen working what we actually get back here in our query is an address builder so if we just die dump on that query we get an address builder why is that well whenever we go ahead and return any kind of builder from our address model we always get back an address builder because new eloquent builder will be invoked and return that for us so now we can take advantage of this
07:34
we can type hint it if we want to just so we are 100 clear so we can type the hint as address builder but now what we can do is just say query and verified and again that auto completes for us because we're type hinting address builder everything's nicely typed and we know that we've got that method in there it's not defined as a scope so now just with that very simple change
07:55
i think this looks a lot cleaner because we're now reusing this method within our user model and of course we have it in our address model we give that refresh we get exactly the same result so of course things can get a lot more complicated than this but once you have out these clearly defined builders rather than lots and lots of scopes floating around you kind of have a reference
08:17
point to each of the methods that you can actually use and this encourages the reusability of everything that you've defined inside of each of your models individual builders so there we go a direct replacement to scopes which has a couple of advantages over scopes and that is by creating out our own custom query builders
4 episodes 23 mins

Overview

Using Eloquent query scopes in your project? Consider swapping them out for custom query builders. Custom query builders are model-specific, class-based builders that provide better organisation, IDE autocompletion, and more.

We'll start this course by defining some standard scopes, refactor them, and discuss the benefits of custom builders.

This course is for you if:

  • You're using query scopes, but they're bloating your models
  • You're working in a team and need more organisation in your project
  • You haven't used query scopes and want a primer, plus an alternative
Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!