【发布时间】:2020-02-21 23:36:51
【问题描述】:
好的,所以我正在构建一个记录一年中每个月的时间条目的表。我有一个名为 $group_months 的变量,如果我转储数据,我会得到:
Collection {#189 ▼
#items: array:4 [▼
"Comp Time Used" => Collection {#294 ▼
#items: array:3 [▼
10 => "12:00:00"
11 => "09:00:00"
"01" => "12:00:00"
]
}
"Vacation Time Used" => Collection {#324 ▼
#items: array:1 [▼
11 => "04:00:00"
]
}
"Sick Time" => Collection {#327 ▼
#items: array:1 [▼
10 => "03:15:00"
]
}
"OT Accrued" => Collection {#318 ▼
#items: array:1 [▼
10 => "12:00:00"
]
}
]
}
我希望能够提取每个集合的标题并将它们用作我的表格的列。所以我想提取“Comp Time Used”作为我表中的一列。然后我想取出“12:00:00”并将其用于我表中的 10 月行。(10 = 10 月)
表格视图:
<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>
时代迁移:
Schema::create('times', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->date('start_day');
$table->text('category');
$table->time('start_time');
$table->time('finish_time');
$table->time('duration');
$table->text('notes');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
});
表控制器:
public function show(User $user)
{
$data = $user->times()->whereYear('start_day', 2019)->get();
$group_months = $data->groupBy(function($entry) {
return $entry->category;
})->map(function($items) {
return $items->groupBy(function($entry) {
return Carbon::parse($entry->start_day)->format('m');
})
->map(function($items) {
$duration = $items->first()->duration;
return $duration;
});
});
return view('table.show', compact('data','group_months'));
【问题讨论】:
-
你能举一个例子,包括更多的数据,包括输入和期望的输出?
-
我编辑了我的原始帖子
-
所以你基本上想改变月份和列名之间的层次结构,例如
['10' => ['Comp Time Used' => '12:00:00', 'Sick Time' => '03:15:00'], 11 => ['Comp Time Used' => '09:00:00', 'Vacation Time Used' => '04:00:00']]?您是否需要每个月的每个列名,如果需要,在没有可用值的情况下,默认值是多少(例如null)? -
是的。我将用我的表格更新我的帖子,以向您展示格式。如果他们没有数据条目让我们说病假,那么集合将不会显示病假的数组。它只会显示其他 3 个值(OT、个人、假期)
-
我更新了我的帖子,提供了更多关于我正在尝试做的事情的信息
标签: php arrays laravel collections