OctoberCMS Builder插件:如何显示关联模型选中项而非全部?
Hey there, let's work through this issue step by step. I’ve dealt with similar association problems in OctoberCMS Builder before, so let’s break down what’s likely going wrong and how to fix it.
1. Double-Check Your Model Relationships in Builder
First, make sure your associations are properly configured in the Builder plugin—this is the foundation of getting related items to show up correctly.
For the Item Model:
- Open your Item model in Builder, go to the Fields tab.
- Add a Relationship field, set its type to Belongs to.
- Under "Related model", select your Category model.
- Ensure the "Foreign key" is set to
category_id(this field should exist in your items database table—Builder usually creates it automatically when you set up the relationship, but double-check!).
For the Category Model:
- Open your Category model in Builder, go to the Fields tab.
- Add a Relationship field, set its type to Has many.
- Under "Related model", select your Item model.
- Set the "Foreign key" to
category_id(this tells OctoberCMS which field in the Item model links back to the Category).
2. Correctly Call Related Items in Your Template
The most common mistake here is trying to loop through all Items instead of accessing the related items directly via the Category model. Here’s how to fix that:
If you’re displaying a single Category (e.g., in a category detail page):
Assuming your template has access to a category variable (from a page component or controller), loop through its related items like this:
<h2>{{ category.name }}</h2> <div class="related-items"> {% for item in category.items %} <div class="item"> <h4>{{ item.title }}</h4> <p>{{ item.description }}</p> </div> {% else %} <p>No items found for this category.</p> {% endfor %} </div>
If you’re displaying a list of Categories with their items:
Nest your loops to iterate through each Category first, then its related Items:
{% for category in categories %} <div class="category"> <h3>{{ category.name }}</h3> <ul> {% for item in category.items %} <li>{{ item.title }}</li> {% else %} <li>No items available.</li> {% endfor %} </ul> </div> {% endfor %}
3. Why the == Filter Isn’t Working
If you tried something like {% for item in items if item.category_id == category.id %}, there are a few possible issues:
- The
categoryvariable might not be in the correct context (e.g., if you’re looping through Categories, make surecategoryrefers to the current iteration). - You’re not using eager loading, which can lead to unexpected behavior (and poor performance!). Instead of filtering manually, use the model association we set up earlier—it’s cleaner and more efficient.
Bonus: Optimize with Eager Loading
To avoid the "N+1 query" problem (where OctoberCMS runs a separate query for each Category’s items), enable eager loading in your component or controller:
- If you’re using a Builder List component for Categories, go to the component’s Model options and add
with: itemsin the "Additional query conditions" field. - If you’re writing custom PHP code (e.g., in a page or component’s
onRunmethod), fetch Categories like this:$this['categories'] = \Author\Plugin\Models\Category::with('items')->get();
4. Test and Debug
- Clear OctoberCMS’s cache (Settings → System → Cache Management) to ensure any model changes take effect.
- Use
{{ dump(category.items) }}in your template to inspect the related items collection—this will show you if the association is actually loading data.
内容的提问来源于stack exchange,提问作者Hessel Kers

