Reviewing Home Route Queries0:27
You can whip up a nice UI to drag and drop and all of that stuff. One nice thing about me being responsible for everything is the code can be sort of my UI. I don't always have to switch over to the front end to add stuff like this. I can even use an array of IDs, and it'll still work just fine. And it's quicker. So let's go to my home route. So this is when you visit the homepage, you get this method. And it's very simple. I fetch 12 of these lessons, 4, 8, 12, and then I fetch 4 testimonials. And you can see those down here, 1, 2, 3, 4.
Dropping the Repository Pattern0:53
I fetch 12 of these lessons, 4, 8, 12, and then I fetch 4 testimonials. And you can see those down here, 1, 2, 3, 4. Okay, so, yeah, notice I keep it very simple. I don't have a Repository here. I did at one point, and then I realized I wasn't necessarily getting any bang for my buck. So I had the repository interfaces, and I had the eloquent repositories and all of that stuff. And, yeah, after a year or two, it just wasn't providing any value for me. So I keep it much simpler. So sometimes I reference the eloquent object directly within the controller. Or other times, if it is something I reuse and I would like to cache it,
Introducing Model Finder Classes1:24
So sometimes I reference the Eloquent object directly within the controller. Or other times, if it is something I reuse and I would like to cache it, then I actually got this trick from Taylor Otwell. On one of his projects, he had a model finder class. And this is good for common queries you make. If you still want to wrap them up, that way you can cache them or things like that, this can be sort of a good stopgap, something in between a full repository and just using the calls directly within your controller. Okay, so here's what getSeries looks like. And notice, like I said, nothing featured about it.
Adding Featured Series Method1:57
Okay, so here's what getSeries looks like. And notice, like I said, nothing featured about it. We take the most recent, in this case, 12 series, and then we cache it so that I don't have to perform the query every single time. But now we're going to do featured series. So back to my home route. And what I could do is maybe add the functionality into this method. But for now, I'm just going to do getFeaturedSeries, maybe something like that. Okay, so right here, getFeaturedSeries. And how do we do this?
Okay, so right here, getFeaturedSeries. And how do we do this? Well, maybe we'll accept an array of IDs. So if you want to be specific about which ones you get, you can pass that in. Otherwise, we won't require it. That way, I can maybe fetch from a config file. I can say IDs equals the array you gave me. Otherwise, just look in the config section. And I have a Laracast config file. And maybe we want a featured array.
Storing Featured IDs in Config2:48
And I have a laracast.php config file. And maybe we want a featured array. Okay, let's create that. So laracast.php. And notice, this should be somewhat familiar. Lots of projects have this. Just the default email, the default support email, how many results I want per page. So yeah, I put a lot of this stuff within configuration files. Now, we're going to add a new section for what I want the featured content to be.
without requiring effort from three or four different people. See what I mean? Anyways, so if we go back, we now have that featured section, which means when I call this find method, I have an array of featured series. Now we know that I want to find those IDs. A lot of people don't know this. You can pass an integer to the find method or an array of integers, in which case Laravel will pick up on that and return to you a collection of the series with these two IDs. So if I pass that through, we're good. But now, yeah, once again, no need to run that query every single time.
So if I pass that through, we're good. But now, yeah, once again, no need to run that query every single time. Let's go ahead and cache it. Return cache remember series.featured. Now in this case, I'm okay hard coding it. But, yeah, if I did find that I'm using this IDs parameter a lot, then I would want to make this more unique. So why don't we cache it for a day at a time? And then let's see. Let's use the IDs. And then here is where I can fetch the series like so.
Let's use the IDs. And then here is where I can fetch the series like so. Okay, so we fetch our IDs. We cache them for one day at a time. And then here's the logic for how we fetch them. So in fact, maybe we could put this directly within here like so. And that way, this will only run once again if we need to recache. So now if we switch back, hopefully this should work. So back to Chrome. If you come back, you can see this is just the most recent 12,
So back to Chrome. If you come back, you can see this is just the most recent 12, some smaller series like charting in you and Russian doll caching in Laravel. But if I give it a refresh now, you can see I'm grabbing only featured content. And I think that's great. So if we wanted to continue on this, yeah, I might decide that there's never really a situation where I would pass in an array of IDs. So if that's the case, I'll remove that entirely. That way I don't have to pass this through. And then we could save the IDs or even inline it like so.
Testing Featured Series Retrieval6:32
That way I don't have to pass this through. And then we could save the IDs or even inline it like so. Now if I come back and give that a refresh, it'll still work just like it did before. Let me add my doc blocks here, retrieve all featured series. And that will return a collection. Now in terms of testing, this is so simple. It's not too important. This will, of course, be picked up in my functional test that I already have running. But if you wanted to do more of a smaller integration test, yeah, you could even create a class that uses this trait,
But if you wanted to do more of a smaller integration test, yeah, you could even create a class that uses this trait, then call the method, and assert what gets returned. And incidentally, for testing, that's when passing through the IDs parameter would help. That way you can circumvent this call to config. So for example, you call the method, and then you just say 1 and 2, which are general factories that you've just created and persisted. And then you can assert that those exact models were returned without having to make this call to the config function. So yes, sometimes when you see stuff like this,
This is just a simple explanation with no code.
