Cleaning Up Controllers0:53
So yeah, good for tutorials, but some queries in your project, yeah, it's going to end up looking kind of like this. And once you get to this point, you really don't want it in your controller. You know what, I guess nothing crazy is going to happen if you keep it there. But you might want to consider a handful of options. One option, if it's going to stay in the controller, is at the very least, extract it to a method. So getCommunityLinks, and then you define a protected method. That would be kind of like a step one, just to clean this up a little bit and give it a name.
Alternative Extraction Options1:20
That would be kind of like a step one, just to clean this up a little bit and give it a name. Another option would be to create a dedicated method on the CommunityLinksController. So you could say CommunityLink, and then getApproved or something like that. And then that would perform your query. However, yeah, still, it ends up being multiple lines. You know, it can end up being 10 or 15 lines. And you may find that you don't even want this on the model. So here's the third option that I'm a big fan of. You take these long queries, these important queries,
Introducing Query Objects1:48
So here's the third option that I'm a big fan of. You take these long queries, these important queries, but long, in-depth ones that probably need to be managed and maintained more than your other queries, and you extract them to their own Query class. Now, a Query object is a class with one responsibility. That is, perform this query. That's all there is to it. So imagine instead, you did something like this. How about CommunityLinksQuery, and give me the results.
Building a Query Class2:11
So imagine instead, you did something like this. How about CommunityLinksQuery, and give me the results. That would be fine. Or if there would be some kind of state or something you want to inject, then of course, you could new it up and then call a public method. Okay, so let's see what this might look like. I'm going to create an app/Queries folder, but you can put it anywhere you want. And we'll call this CommunityLinksQuery. Okay, so let's set our namespace, and then the class. So we have a static method called get, right?
Okay, so let's set our namespace, and then the class. So we have a static method called get, right? And I'll paste that in, like so. And then we will return the results. And then we would use this at the top. So use app\CommunityLink. And you get the basic idea. I have a couple variables here that could be defined elsewhere. This is actually real code. But yeah, you could pass in the channel here.
Testing and Reusing Queries2:59
This is actually real code. But yeah, you could pass in the channel here. You could determine what the orderBy should be. In this case, let's just hard code it for the example. And now, when it comes to testing, yeah, I mean, this is just a long SQL query, essentially. So you would write a simple little unit test that touches the database, or you can call it an integration test if you want, where you build up a couple factories. You trigger this method.
where you build up a couple factories. You trigger this method. And then you make sure that what gets returned is what you expect. It's very, very simple. And now, notice that in your controller, yeah, now notice that all of these dozen lines are now in their own dedicated class that can be reused anywhere. And in your controller, you just reference a single call. And of course, once again, you'll need to import that at the top. And that's it.
And of course, once again, you'll need to import that at the top. And that's it. That's Eloquent query objects in a nutshell. You take kind of a sloppy SQL query or Eloquent query. And if you don't really want it in the controller or the model itself, you can extract it to its own class and then just adopt any kind of interface here that you want, like get or run or trigger. You get the basic idea. Okay, that's it.
You get the basic idea. Okay, that's it. So that's query objects in a nutshell.
