In this episode, we take what might sound like a boring step—defining the inverse of a many-to-many relationship in Laravel—but it actually unlocks a ton of new possibilities for your app.
You’ll see how adding this inverse relationship to your Topic
model (so that a topic can easily retrieve all its courses) lets us build cool new URLs like /topics/laravel
to filter all courses under the "Laravel" topic. We walk through setting up that relationship, building a quick test route, and then building out a Blade view for showing all courses in a given topic.
After that, we take it a step further, showing how you might display not just the courses under a topic, but also, for each course, every topic that course belongs to. This is where things can get slow and inefficient if you’re not careful, so we revisit Eloquent eager loading to avoid the classic N+1 query problem. There's a nice practical example of loading nested relationships using dot notation, and you’ll see firsthand how much more efficient that can make things.
By the end, you won't just have a new relationship in your models—you'll have the foundation for snappier, more useful topic pages, with performance taken care of, too.