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!

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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀