Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel-填充选择optgroup下拉列表,其中按名称记录组_Php_Mysql_Laravel 4 - Fatal编程技术网

Php Laravel-填充选择optgroup下拉列表,其中按名称记录组

Php Laravel-填充选择optgroup下拉列表,其中按名称记录组,php,mysql,laravel-4,Php,Mysql,Laravel 4,我正在尝试在Laravel中填充一个select下拉列表,以显示产品的属性 <select> @foreach($attributes as $key=>$attr) <optgroup label="{{$attr->name}}"> <option value="{{$attr->value}}">{{$attr->value}}</option> </optgroup> @e

我正在尝试在Laravel中填充一个
select
下拉列表,以显示产品的属性

<select>
  @foreach($attributes as $key=>$attr)    
  <optgroup label="{{$attr->name}}">
    <option value="{{$attr->value}}">{{$attr->value}}</option>
  </optgroup>
  @endforeach
</select>
尽管如此,我需要根据它们的名称查找类似的记录,例如
颜色
select
下拉列表应在
中列出一次
$attr->name
,而
$attr->value
应在
中重复

我试图更改查询并使用
groupBy
,但这不会像预期的那样起作用。查询将只返回任何记录的第一个实例,其余的将被忽略

$attributes = Attribute::orderBy('name', 'ASC')->groupBy('name')->get();

我怎样才能解决这个问题?先谢谢你

我会将所有属性放入多维数组,然后迭代生成select

// Fetch all attributes
$results = Attribute::orderBy('name', 'ASC')->get();
$attributes = array();
/**
 *  Group results into a multidimensional array like:
 *  [color]
 *      [red]
 *      [white]
 *  [shape]
 *      [circle]
 *      [square]
 */
foreach ( $results as $v ) {
    if ( !isset($attributes[$v->name]) ) {
        $attributes[$v->name] = array();
    }
    $attributes[$v->name][$v->value] = $v->value;
}
// Spit out the select/option groups
?>
<select>
      @foreach ( $attributes as $key => $attr )    
      <optgroup label="{{$key}}">
        @foreach ( $attr as $values )
            <option value="{{$value}}">{{$value}}</option>
        @endforeach
      </optgroup>
      @endforeach
</select>
//获取所有属性
$results=Attribute::orderBy('name','ASC')->get();
$attributes=array();
/**
*将结果分组到多维数组中,如:
*[颜色]
*[红色]
*[白色]
*[形状]
*[圆圈]
*[广场]
*/
foreach(结果为$v){
如果(!isset($attributes[$v->name])){
$attributes[$v->name]=array();
}
$attributes[$v->name][$v->value]=$v->value;
}
//吐出选择/选项组
?>
@foreach($key=>$attr的属性)
@foreach($attr作为$value)
{{$value}}
@endforeach
@endforeach

我会将所有属性放入多维数组中,然后迭代生成select

// Fetch all attributes
$results = Attribute::orderBy('name', 'ASC')->get();
$attributes = array();
/**
 *  Group results into a multidimensional array like:
 *  [color]
 *      [red]
 *      [white]
 *  [shape]
 *      [circle]
 *      [square]
 */
foreach ( $results as $v ) {
    if ( !isset($attributes[$v->name]) ) {
        $attributes[$v->name] = array();
    }
    $attributes[$v->name][$v->value] = $v->value;
}
// Spit out the select/option groups
?>
<select>
      @foreach ( $attributes as $key => $attr )    
      <optgroup label="{{$key}}">
        @foreach ( $attr as $values )
            <option value="{{$value}}">{{$value}}</option>
        @endforeach
      </optgroup>
      @endforeach
</select>
//获取所有属性
$results=Attribute::orderBy('name','ASC')->get();
$attributes=array();
/**
*将结果分组到多维数组中,如:
*[颜色]
*[红色]
*[白色]
*[形状]
*[圆圈]
*[广场]
*/
foreach(结果为$v){
如果(!isset($attributes[$v->name])){
$attributes[$v->name]=array();
}
$attributes[$v->name][$v->value]=$v->value;
}
//吐出选择/选项组
?>
@foreach($key=>$attr的属性)
@foreach($attr作为$value)
{{$value}}
@endforeach
@endforeach