In this episode, we're digging into why our IDE isn't auto-completing custom methods like hasVerifiedEmail
on our query builder. We start by looking at the default Eloquent builder and notice that things like where
are auto-completed, and that's mainly due to Laravel's base model typehinting its return values properly.
But when we switch over to our custom UserBuilder
, the IDE loses track because it doesn't know we're actually returning our own builder. So, to fix this, we override the query
method in our model, make sure it calls parent::query()
and, most importantly, we add a return typehint to indicate that this returns a UserBuilder
.
After doing this, our IDE starts playing nice—now when we call query()->hasVerifiedEmail()
, we get proper auto-complete suggestions, which makes working with these kinds of custom builders way smoother.
We wrap things up by pointing out the opposite: before typehinting, the auto-completion just wasn't there, but now it recognizes all custom methods on our builder (even though they technically aren't scopes). In the next episode, we’ll go a bit deeper and see what happens when we have multiple models each with their own builders.