Laravel 5.6三个模型关联实现:按分类展示狗狗事件列表
Hey Pierre, great question! Let's walk through how to build this feature where each category gets its own table of events on a dog's detail page. Here's a step-by-step breakdown using a common Laravel stack (the core logic applies to other frameworks too):
Make sure your ORM models have the correct associations set up so we can easily fetch related data:
Dog Model
class Dog extends Model { // A dog has many events public function events() { return $this->hasMany(Event::class); } }
Event Model
class Event extends Model { // An event belongs to one dog and one category public function dog() { return $this->belongsTo(Dog::class); } public function category() { return $this->belongsTo(Category::class); } }
Category Model
class Category extends Model { // A category has many events public function events() { return $this->hasMany(Event::class); } }
In your dog detail controller method, fetch the specific dog along with its events and their associated categories. Then group the events by their category to make rendering easier:
public function show($id) { // Fetch the dog with preloaded events and categories (avoids N+1 queries) $dog = Dog::with(['events.category'])->findOrFail($id); // Group events by category ID $eventsByCategory = $dog->events->groupBy('category.id'); return view('dogs.show', compact('dog', 'eventsByCategory')); }
Now in your template, loop through each grouped category and render a dedicated table for its events:
<div class="dog-detail-container"> <h1>{{ $dog->name }}'s Event History</h1> <p><em>Born: {{ $dog->birthday->format('F j, Y') }} | Puce: {{ $dog->puce }}</em></p> <!-- Loop through each category group --> @foreach($eventsByCategory as $categoryEvents) <!-- Grab category info from the first event in the group --> <?php $category = $categoryEvents->first()->category; ?> <h2 class="category-header">{{ $category->name }}</h2> <table class="event-table"> <thead> <tr> <th>Event Name</th> <th>Date</th> </tr> </thead> <tbody> <!-- Render each event in the category --> @foreach($categoryEvents as $event) <tr> <td>{{ $event->name }}</td> <td>{{ $event->date->format('F j, Y') }}</td> </tr> @endforeach <!-- Handle empty category cases --> @if($categoryEvents->isEmpty()) <tr> <td colspan="2" class="empty-state">No events recorded for this category.</td> </tr> @endif </tbody> </table> @endforeach <!-- Handle case where the dog has no events at all --> @if($eventsByCategory->isEmpty()) <p class="no-events">No events have been added for {{ $dog->name }} yet.</p> @endif </div>
- Django: Use
prefetch_relatedto load events and categories, then use Python'sitertools.groupbyin your view to group events by category. Loop through the groups in your Jinja2 template. - Vanilla JS/React/Vue: If fetching via API, return grouped events from the backend, or group client-side using
Array.reduce()before rendering.
The core idea is always the same: group the dog's events by their category, then render a table for each group.
内容的提问来源于stack exchange,提问作者pierre

