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
03. Typehinting the builder

Transcript

00:00
Let's think about why, at the moment, our IDE isn't auto-completing this hasVerifiedEmail method for us.
00:06
Now, when we use the QueryBuilder, as we've seen before, I can start typing things like WHERE, and my IDE can auto-complete this and give me all of the information about the arguments that we need. So that's really good, but why is that?
00:19
Well, that's because if we open up the base model, so inside of Eloquent, and we look for QUERY, this goes ahead and explicitly returns something. Now, my IDE can work out what that is, because eventually,
00:31
when we return this new query, if we follow this down, we get a builder instance. And those methods are defined on the builder. However, when we are over in our user model,
00:43
and we have this UserBuilder, my IDE doesn't quite know what's going on, because NewEloquentBuilder, we're not using directly. So let's go ahead and tidy this up by also overriding the Query method, which we are using.
00:57
So we're going to go ahead and create our static function QUERY, because we always call this statically, and we're going to go ahead and return ParentQuery. Now, that's not quite going to be enough,
01:08
because in here, we'd think, well, we could just return a new UserBuilder, surely. Not quite, just because of the way that Larafeld passes this through. Let's go over to web and just see what happens here.
01:19
So we're going to go ahead and say QUERY, and let's say HasVerifiedEmail, still not quite working. Now, we can make this work, if we just pull this back again to a working example,
01:30
by type hinting the return value of QUERY, which we know is eventually going to be a UserBuilder. So let's go ahead and type in the return value of this to UserBuilder. And while we're here, we may as well also type in this as well.
01:46
So now, my IDE will know that we're returning a UserBuilder. Let's try this again. So QUERY, HasVerifiedEmail, and there we go. We got an auto-completed method for that HasVerifiedEmail scope.
02:01
And I say scope, but of course, it isn't a scope, it's a method on our custom QueryBuilder. So now, that's much more convenient, and we can continue to chain on any of the methods that we have within our builder,
02:14
or of course, we can go and continue to chain on other methods that we have inside of our builder. For this though, of course, I'm just going to go ahead and say Get, give that a refresh, and we get the results that we've seen already.
02:26
To finish things up in the next episode, let's look at a slightly more complex example with using two different models, two different builders, and kind of combining these together.
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!