You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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):

1. First, Define Model Relationships

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);
    }
}
2. Fetch & Group Data in the Controller

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'));
}
3. Render the Tables in Your View

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>
Quick Notes for Other Frameworks
  • Django: Use prefetch_related to load events and categories, then use Python's itertools.groupby in 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:18:59