Having watched the beginner Laravel series here, read the docs and watched a few other videos I've got most of the basics down (I think!) but something I've not seen covered anywhere is when you want to display related data on a seperate page to the main data. I know how to get related data defining the model relations, but any example I've seen just uses that relation on the same primary page/view.
Take a generic example, if you had events of some sort and a relation of people that were at each event, you'd have a M2M relationship set up on the models and an event_person pivot table.
//Event.php Model
public function people()
{
return $this->belongsToMany(Person::class);
}
//Person.php Model
public function events()
{
return $this->belongsToMany(Event::class);
}
Then you'd set up the route and controller (Or do it as a resource controller, whatever)
Route::get('/events/{event}', [EventController::class, 'show']);
Then the show function in the controller would be something like:
public function show(Event $event)
{
return view('events.show', [
'event' => $event->load('people'),
]);
}
Then I could display the people on that view, fine. But what if instead I'd like to display the people at a dedicated /events/123/people sub-page, based on a different view? And in a real world, there might be several of these sub pages, potentially all with a completely different layout/view needed.
I can think of several ways to do this but it seems like a pretty common enough scenario that there is a convention I should be following?
- Have the extra route(s) use the same Event@Show method and use if statements on the controller to return a different view and data depending on the route request?
- Creating extra methods in the EventController, i.e
eventPeople()?
- Creating a
PersonEventController and so on?
Or something else I'm not thinking of? I'm leaning towards the first option but would appreciate some guidance to make sure I'm following the conventions, thanks!