我如何使用 octobercms 中的选项填充 select 框

How can i populate select box with options in octobercms

我的控制器里有这个

$this['item'] = Cat::where('parent_id',0)->pluck('cat_title');

和这个观点

 <select id="inputCat" class="form-control">
    <option selected>Choose...</option>

    {% for item in item %}
    <option value={{ item.id }}>{{ item.cat_title }}</option>
    {% endfor %}

  </select>

它在选项中指示但不显示选项。 我做错了什么?

我猜你正在用 cat_title 采摘 Cat 所以返回数组将 Notidcat_title 作为键你指的是 valueoption 所以,可能是它引起的问题

这样做,

$this['items'] = Cat::where('parent_id',0)->pluck('cat_title', 'id');
//       ^ plural [ better approach ]      id as well for value  ^

返回数组;

Array[
    1 => 'title'
//  ^ id   ^ your cat_title
    ....
]

this will be returned by pluck() so you can see no item.id nor item.cat_title is there. its just key and value pair.

所以对于标记请使用


<select id="inputCat" class="form-control">
    <option selected>Choose...</option>
    {% for key, item in items %}
        <option value={{ key }}>{{ item }}</option>
    {% endfor %}
</select>

如果您发现问题或不起作用,请尝试此操作,然后请发表评论。