doucai4274 2017-09-12 07:06
浏览 82
已采纳

如何根据Laravel 5.4中的属性名称对dropdown进行分组?

One product has multiple attributes, each combination represent an individual product. As you add variable products with variations in woocommerce (WordPress).

Now I've product attributes in array and confused how can I create multiple drop downs based on multiple categories.

array:3 [▼
  0 => array:3 [▼
    "id" => 13
    "name" => "size"
    "value" => "small"
  ]
  1 => array:3 [▼
    "id" => 14
    "name" => "size"
    "value" => "large"
  ]
  2 => array:3 [▼
    "id" => 19
    "name" => "color"
    "value" => "white"
  ]
]

As index 0, 1 has same name "Size" having different values, so it should display in a one dropdown and other one should display on another drop down.

I know my logic is wrong here to print out drop downs can you guys help me ?

<select class="form-control" id="attribute" name="attribute[]" style="width:70%;">
@foreach($attributes as $attribute)
<option value="">Select {{  $attribute->name }}</option>
<option value="{{ $attribute->id }}">{{ $attribute->value }}</option>
@endforeach
</select>

Thanks,

  • 写回答

1条回答 默认 最新

  • duancuan7057 2017-09-12 07:23
    关注

    You could use collection to group the attributes by their type and then loop through them that way:

    @foreach(collect($attributes)->groupBy('type') as $options)
    
        <label>{{ $options->first()->name }}</label>
    
        <select class="form-control">
            @foreach($options as $option)
                <option value="{{ $option->id }}">{{ $option->value }}</option>
            @endforeach
        </select>
    @endforeach
    

    You will need to sort the name attribute for the selects.

    Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路