简体   繁体   English

Laravel - 如何将集合数据加载到表中?

[英]Laravel - How can I load collection data into a table?

I'm storing my data with a variable called $group_months, the following is when I dd($group_months).我用一个名为 $group_months 的变量存储我的数据,以下是我 dd($group_months) 时的情况。 The numbers before the arrow representative the month, and the numbers after are the time.箭头前面的数字代表月份,后面的数字是时间。 So 10 = October所以 10 = 十月

Collection {#187 ▼
  #items: array:4 [▼
    "Comp Time Used" => Collection {#292 ▼
      #items: array:3 [▼
        10 => "12:00:00"
        11 => "09:00:00"
        "01" => "12:00:00"
      ]
    }
    "Vacation Time Used" => Collection {#322 ▼
      #items: array:1 [▼
        11 => "04:00:00"
      ]
    }
    "Sick Time" => Collection {#325 ▼
      #items: array:1 [▼
        10 => "03:15:00"
      ]
    }
    "OT Accrued" => Collection {#316 ▼
      #items: array:1 [▼
        10 => "12:00:00"
      ]
    }
  ]
}

I want to use this data and fill up my table that looks like this:我想使用这些数据并填写我的表格,如下所示:

<table class="table table-striped table-sm">
            <thead>
              <tr>
                <th scope="col">Month</th>
                <th scope="col">Overtime Hours</th>
                <th scope="col">Compensation Hours</th>
                <th scope="col">Vacation</th>
                <th scope="col">Personal Hours</th>
                <th scope="col">Sick Hours</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <th scope="row">Jan</th>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td>
              </tr>
              <tr>
                <th scope="row">Feb</th>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
              </tr>
              <tr>
                <th scope="row">Mar</th>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
              </tr>
              <tr>
                <th scope="row">Apr</th>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
              </tr>
              <tr>
                <th scope="row">May</th>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
              </tr>
              <tr>
                <th scope="row">Jun</th>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
              </tr>
              <tr>
                <th scope="row">Jul</th>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
              </tr>
              <tr>
                <th scope="row">Aug</th>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
             </tr>
             <tr>
                <th scope="row">Sep</th>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <th scope="row">Oct</th>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <th scope="row">Nov</th>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <th scope="row">Dec</th>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            </tbody>
 </table>

I'm not sure how to do this, maybe use some sort of loop?我不知道该怎么做,也许使用某种循环? Thanks for the help.谢谢您的帮助。

Edit: Storing collection as an array now:编辑:现在将集合存储为数组:

array:4 [▼
  "Comp Time Used" => array:3 [▼
    10 => "12:00:00"
    11 => "09:00:00"
    "01" => "12:00:00"
  ]
  "Vacation Time Used" => array:1 [▼
    11 => "04:00:00"
  ]
  "Sick Time" => array:1 [▼
    10 => "03:15:00"
  ]
  "OT Accrued" => array:1 [▼
    10 => "12:00:00"
  ]
]

This should get you started.这应该让你开始。 I would combine all the times in the controller before sending it to the view.在将其发送到视图之前,我会将 controller 中的所有时间结合起来。 That would make it easy.那会很容易。 Here, if the array key exists for that month, it'll print the value for that month.在这里,如果该月存在数组键,它将打印该月的值。 If it does not exist, it won't throw an error because array_key_exists will just return false and do nothing else.如果它不存在,它不会抛出错误,因为array_key_exists只会返回 false 并且什么都不做。

<table class="table table-striped table-sm">
            <thead>
              <tr>
                <th scope="col">Month</th>
                <th scope="col">Overtime Hours</th>
                <th scope="col">Compensation Hours</th>
                <th scope="col">Vacation</th>
                <th scope="col">Personal Hours</th>
                <th scope="col">Sick Hours</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <th scope="row">Jan</th>
                <td></td>
                <td></td>
                @if(array_key_exists(1, $group_months['Comp Time Used']))
                <td>{{$group_months['Comp Time Used'][1]</td>
                @else
                <td>No time off used</td>
                @endif
                <td></td>
                <td></td>
                <td></td>
              </tr>
              <tr>
                <th scope="row">Feb</th>
                <td></td>
                <td></td>
                @if(array_key_exists(2, $group_months['Comp Time Used']))
                <td>{{$group_months['Comp Time Used'][2]</td>
                @else
                <td>No time off used</td>
                @endif
                <td></td>
                <td></td>
                <td></td>
              </tr>
            </tbody>
 </table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM