Ad

How Can I Populate Select Box With Options In Octobercms

- 1 answer

I have this in my controller

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

and this in view

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

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

  </select>

It indicates in the options but doesn't show the options. What am i doing wrong?

Ad

Answer

I guess you are plucking Cat with cat_title so returning array will Not have id or cat_title as key which you are referring for value and option so, may be its causing issue

Do like this,

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

Returning Array;

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.

So for markup please use


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

please try this if you find issue or it did not work, then please comment.

Ad
source: stackoverflow.com
Ad