Enjoying learning laravel with the beginners course but something not really covered was filtering, particularly with full routes.
I understand the 7 default methods on a controller, and index is where you'd return all your items, whatever they may be. But what if I want to filter those full results via a clean proper route, not a ?foo=bar query string? So they'd still be shown with the same view, exact same layout, but filtered by whatever.
For example in a blog or news website I might return all the unfiltered articles on the root or /articles but then also have filtered routes like:
- /articles/{year} to get all articles published in X year
- /articles/{author} to get all articles by X author
- /articles/{category} to get all articles in X category
...and so on.
What would the broad approach to that be? Would you use the existing Article controller, or create a new one for each? That seems unlikely to be the best approach but the only similarlity in the video series I saw was when Jeffrey made the Tag controller for tags.
So far I've just tried to get /articles/{category} working by adding a category_id to the articles table, then a categories table with a name and foreign id, plus all required relationships in the models.
I can then get the results I want with $articles = $category->articles which works, but it's not eager loading like that and I seemingly can't chain things like paginate onto it like that either. So I then tried constructing a full Eloquent query but got stuck in the fact that the where clause I wanted was on the subtable, not the table of the Article model. i.e. I was passing the category word in the route (articles/opinion) rather than the id in the primary table. Couldn't work out how you'd manage that.
I think I went down a few too many rabbit holes via google as a beginner and confused myself, so some proper guidance on the general principles here would be very helpful! I feel like I must be overcomplicating what seems like a simple filtering action with a route.