代码之家  ›  专栏  ›  技术社区  ›  pmiranda

属性Laravel对象的名称

  •  0
  • pmiranda  · 技术社区  · 6 年前

    我在拉雷维尔有一个物体:

    array:144 [▼
      0 => {#10 ▼
        +"year": 2005
        +"month": "ene"
        +"FOO": "37"
        +"BAR": "17"
      }
      1 => {#11 etc...
    

    我需要把“年”、“月”、“FOO”和“BAR”的名字放在表格中:

    <thead>
        <tr>
            @foreach($data as $column_name)
                <th><strong>{{ $column_name }}</strong></th>
            @endforeach
        </tr>
    </thead>
    

    $data是对象。

    因此,该表应该如下所示:

    year | month | FOO | BAR
    ------------------------
    2005    ene    37   17
    

    这些值工作正常,但我不知道如何检索属性的名称来构建

    编辑:

    这是我从一些答案中得到的:

    enter image description here

    2 回复  |  直到 6 年前
        1
  •  0
  •   fubar    6 年前

    如果 $data 是第一个 stdClass 中的对象 array

    <thead>
        <tr>
            @foreach((array) $data[0] as $key => $value)
                <th><strong>{{ $key }}</strong></th>
            @endforeach
        </tr>
    </thead>
    
        2
  •  0
  •   kerrin    6 年前

    试试这个:

    <thead>
        <tr>
            @foreach($data as $key)
                <th><strong>{{ $key['year'] }}</strong></th>
            @endforeach
        </tr>
    </thead>
    

    更新:

    <thead>
        <tr>
            @foreach($data as $key)
                <th><strong>{{ $key->year }}</strong></th>
            @endforeach
        </tr>
    </thead>