Playing
06. Better IDE support

Transcript

00:00
Generally, when we invoke the QueryBuilder first before we start to train any methods on, it gives us better IDE support. Now, of course, this depends on the IDE that you're using. Whether you have any particular packages of your IDE that you've pulled in which assist
00:13
with this regardless of using the QueryBuilder method, most people will find it beneficial to use the QueryBuilder method just so you can click through to inspect things. So let's go ahead and look at an example of this. So I'm just going to define out a POST variable.
00:28
We'll just say WHERE slug, slug1, like we've seen before. And we'll go ahead and call that FIRST or FIRSTORFAIL method. And let's just die dump on that POST here. So let's come back over to our browser, give that a refresh, and yeah, we get 404 not found.
00:44
So let's see what's going on here. That's POST1, not slug1. There we go. So we've got it in there.
00:52
Now, with my editor, I can hold down my Option key and click on any of these methods. At the moment, I can't, though, because we're using that call magic method that we looked at in the first episode. This doesn't know where it's coming from.
01:06
I don't have anything installed in my editor specifically that allows me to inspect this. It doesn't recognize it within Laravel. So if we bring in that QueryBuilder method, which we know returns a new builder instance, my IDE now knows what these methods are and where they belong.
01:25
So I can click on this and go directly through to this so I can see what this takes in. I can review the code just to see how this works. I can click on this and see what happens when this works. And of course, this is very simple, but once things start to get a little bit more complicated,
01:41
this can be really, really useful for you to just jump straight over to these builder methods. So you might find some sort of package that handles this for you specifically within Laravel, or you can just go ahead and use the QueryBuilder method to get this support within your IDE.

Episode summary

In this episode, we talk about improving your IDE experience when working with Laravel's QueryBuilder. We start by showing why calling the QueryBuilder explicitly (instead of chaining methods straight off the model) can help your IDE—like VSCode or PhpStorm—understand what methods are available, letting you click through and inspect their definitions more easily.

There's a quick demo where we define a variable by querying for a post with a certain slug. At first, when chaining methods directly from the model, our IDE can't follow where these methods are coming from because of some PHP magic happening in Laravel. But if we use the QueryBuilder method, our IDE recognizes the builder instance, and all those nifty jump-to-definition and code inspection features start working.

Finally, we mention that while some IDEs or plugins may add extra support for Laravel, using the QueryBuilder directly is a quick, universal way to help your IDE help you—making it way easier to read and understand complex queries as your app grows.

Episode discussion

No comments, yet. Be the first!