Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.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-如何采集嵌套集合?_Php_Arrays_Laravel_Collections - Fatal编程技术网

Php Laravel-如何采集嵌套集合?

Php Laravel-如何采集嵌套集合?,php,arrays,laravel,collections,Php,Arrays,Laravel,Collections,好的,我正在建立一个表,记录一年中每个月的时间条目。我有一个名为$group_months的变量,如果我转储数据,我会得到以下结果: Collection {#189 ▼ #items: array:4 [▼ "Comp Time Used" => Collection {#294 ▼ #items: array:3 [▼ 10 => "12:00:00" 11 => "09:00:00" "01" =&g

好的,我正在建立一个表,记录一年中每个月的时间条目。我有一个名为$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”去掉,用它来表示我桌上的十月行

TableView:

<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>
TableController:

 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'));

您当前正在查询所有数据,并将其分组到内存中,这并不是很有效。相反,您应该尽可能多地推送到数据库。这也意味着只查询相关数据:
类别
月份
总和(持续时间)

我们可以使用此数据以通用方式生成表,使用数组索引作为月号

<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>
        @foreach($data as $month => $row)
            <tr>
                <th scope="row">{{ $month }}</th>
                <td>{{ $row['Overtime Hours'] ?? '-' }}</td>
                <td>{{ $row['Compensation Hours'] ?? '-' }}</td>
                <td>{{ $row['Vacation'] ?? '-' }}</td>
                <td>{{ $row['Personal Hours'] ?? '-' }}</td>
                <td>{{ $row['Sick Hours'] ?? '-' }}</td>
            </tr>
        @endforeach
    </tbody>
</table>

你能给我一个输入和输出数据都多一点的例子吗?我编辑了我的原始帖子,所以你基本上想改变月份和列名之间的层次结构,例如
['10'=>['Comp Time Used'=>'12:00:00','Sick Time'=>'03:15:00'],11=>['Comp Time Used'=>'09:00:00','休假时间Used'=>'04:00']
?您是否需要每个月的每个列名称?如果需要,在没有可用值的情况下,默认值是什么(例如,
null
)?是的。我会用我的表格更新我的帖子,向您显示格式。如果他们没有病假数据条目,那么集合不会显示病假数组。它只会显示其他3个值(加班、个人、假期)我更新了我的帖子,提供了更多关于我想做的事情的信息。太棒了。感谢你花时间给出如此详细和清晰的代码响应。表格工作正常,只是每一行都显示“-”,而不是记录的时间,你知道为什么吗?即使没有记录,也有办法在表格中显示每个月吗按顺序排列的值,因为现在只显示有时间项的月份。@stormakt您需要将行列与表中的确切类别交换,例如,
$row['Vacation time Used']
如您的问题中所述。我不知道所有这些,所以我只是使用表中的列标题作为索引。--我在答案中添加了一个带填充月份的扩展版本。但这很复杂。:)我更新了数据库,从我的时间表中删除了category列,并添加了categories表。我正在引用表中的ID表。我创建了一个新线程,因为我遇到了数据未显示的问题。您能帮我解决吗?
public function show(User $user) 
{
    $data = $user->times()
        ->whereYear('start_day', 2019)
        ->groupBy('category', \DB::raw('month(start_date)'))
        ->select([
            'category',
            \DB::raw('month(start_date) as month'),
            \DB::raw('sum(duration) as duration'),
        ])
        ->orderBy(\DB::raw('month(start_date)'))
        ->get()
        ->mapToGroups(function ($item) {
            $month = \DateTime::createFromFormat('!m', $item['month'])->format('M');
            return [$month => [$item['category'] => $this->formatDuration($item['duration'])]];
        })
        ->mapWithKeys(function ($item, $key) {
            return [$key => $item->collapse()];
        });

    return view('table.show', compact('data'));
}

private function formatDuration($seconds)
{
    $duration = '';

    if ($seconds < 0) {
        $duration = '-';
        $seconds  = abs($seconds);
    }

    $hours    = floor($seconds / 3600);
    $seconds -= $hours * 3600;
    $minutes  = floor($seconds / 60);
    $seconds -= $minutes * 60;

    return $duration . sprintf('%d:%02d:%02d', $hours, $minutes, $seconds);
}
[
    'Jan' => [
        'Category 1' => '1:20:40',
        'Category 2' => '15:05:40',
        'Category 4' => '0:00:50'
    ],
    'Feb' => [
        'Category 2' => '2:30:15',
        'Category 3' => '4:45:30'
    ]
]
<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>
        @foreach($data as $month => $row)
            <tr>
                <th scope="row">{{ $month }}</th>
                <td>{{ $row['Overtime Hours'] ?? '-' }}</td>
                <td>{{ $row['Compensation Hours'] ?? '-' }}</td>
                <td>{{ $row['Vacation'] ?? '-' }}</td>
                <td>{{ $row['Personal Hours'] ?? '-' }}</td>
                <td>{{ $row['Sick Hours'] ?? '-' }}</td>
            </tr>
        @endforeach
    </tbody>
</table>
public function show(User $user) 
{
    $data = collect(array_fill(1, 12, []))
        ->replace(
            $user->times()
                ->whereYear('start_day', 2019)
                ->groupBy('category', \DB::raw('month(start_date)'))
                ->select([
                    'category',
                    \DB::raw('month(start_date) as month'),
                    \DB::raw('sum(duration) as duration'),
                ])
                ->orderBy(\DB::raw('month(start_date)'))
                ->get()
                ->mapToGroups(function ($item) {
                    return [$item['month'] => [
                        $item['category'] => $this->formatDuration($item['duration'])
                    ]];
                })
                ->mapWithKeys(function ($item, $key) {
                    return [$key => $item->collapse()];
                })
        )
        ->mapToGroups(function ($item, $key) {
            $month = \DateTime::createFromFormat('!m', $key)->format('M');
            return [$month => $item];
        });

    return view('table.show', compact('data'));
}